Error "502: API Services rejected request with error. HTTP 429" in Pages:AdvanceToNextPageAsync()

I have this loop to remove all keys from a DataStore:

DSoptions.AllScopes = true
local DataStore = DataStoreService:GetDataStore(DS, "", DSoptions)

local Sucess, Pages = pcall(function()
	return DataStore:ListKeysAsync('global')
end)
if Sucess then
	while true do
		local Items = Pages:GetCurrentPage()
		for _, Key in ipairs(Items) do
			local Sucess, Error = pcall(function()
				local Result = DataStore:RemoveAsync(Key.KeyName)
			end)

			if not Sucess then
				warn(Error)
			end
		end
		if Pages.IsFinished then
			break
		end
		Pages:AdvanceToNextPageAsync()
	end
end

Many times I’m getting this error:
image
Which is correspondent with the line:

Pages:AdvanceToNextPageAsync()

What’s wrong?

1 Like

You are seeing this: 429 (too many requests). In the context of datastores, you are removing too many keys at once without a cooldown. Maybe add a task.wait() to your loop.

One thing I want to note out here, you are better off changing the name of the datastore name, it saves more time and has a less chance of failing unlike loops.

For example, if all the data you are trying to clear is in a DataStore called “PlayerData_1”, change it to something like “PlayerData_2”. Now, “PlayerData_2” has no data inside, its a fresh start.

However if you want to continue with this method try this script

task.wait(3)

local DSS = game:GetService("DataStoreService")

local DataStorePages = DSS:ListDataStoresAsync()

local dataClear = coroutine.wrap(function()
    while true do
        local CurrentStoresPage = DataStorePages:GetCurrentPage()
    
        for i, DataStoreInfo in pairs(CurrentStoresPage) do
    
            local DataStore = DSS:GetDataStore(DataStoreInfo.DataStoreName)
            local KeysPages = DataStore:ListKeysAsync()
    
            while true do
                local CurrentKeysPage = KeysPages:GetCurrentPage()
    
                for j, Key in pairs(CurrentKeysPage) do
                    local KeyStore = DataStore:GetAsync(Key.KeyName)
                    print("deleting " .. Key.KeyName .. "...")
                    DataStore:RemoveAsync(Key.KeyName)
                end
    
                if KeysPages.IsFinished then break end
    
                KeysPages:AdvanceToNextPageAsync()
            end
        end
    
        if DataStorePages.IsFinished then break end
    
        DataStorePages:AdvanceToNextPageAsync()
    
        task.wait() -- Buffer
    end
end)

dataClear()

print("done deleting data")

(not sure if this will work with your project).

For reference:

1 Like

In my case, the solution was to wrap Pages:AdvanceToNextPageAsync() inside a pcall:

repeat
	local Success, Errormessage = pcall(function()
		Pages:AdvanceToNextPageAsync()
	end)
	if not Success then
		warn('Error Pages:AdvanceToNextPageAsync. Trying again')
		warn(Errormessage)
		wait(1)
	end
until Success