How Can I Get All Keys of a DataStore in Table Form?

I want all the Keys of a DataStore. The Values of the DataStore are Tables.

I plan on going through every DataStore I’ve ever used, Using DataStoreService:ListDataStoreAsync() to get the Names. And getting the Keys which I will then Perform DataStore:RemoveAsync() on to remove them.

So far I’ve tried DataStore:GetOrderedDataStore() to get Pages which I can loop through but it cant seem to sort the datastore cause the values aren’t Numbers but Tables.

  23:01:45.634  {}  -  Server - ResetDataStoresScript:85
  23:01:46.818  {}  -  Server - ResetDataStoresScript:85
  23:01:48.084  {}  -  Server - ResetDataStoresScript:85
  23:01:49.267  {}  -  Server - ResetDataStoresScript:85

I’ve only found one other person who has asked the same question on the DevForums, Here. But that post never got anywhere.

My Code Looks Something Like This:

local DataStoreService = game:GetService("DataStoreService")

local Success, DataStorePages = pcall(function()
	return DataStoreService:ListDataStoresAsync()
end)
if not Success then return end


-- Gets Data Store Names:

local DataStoreNames = {}
while task.wait() do
	
	local DataStorePage = DataStorePages:GetCurrentPage()
	
	for _, DataStore in pairs(DataStorePage) do
		table.insert(DataStoreNames, DataStore.DataStoreName)
	end
	
	if DataStorePages.IsFinished then break else DataStorePages:AdvanceToNextPageAsync() end
	
end


-- Gets Data Of DataStores:

for _, DataStoreName in ipairs(DataStoreNames) do
	
	local DataStore = DataStoreService:GetOrderedDataStore(DataStoreName)
	
	local DataPages = DataStore:GetSortedAsync(false, 10)
	
	while task.wait(1) do
		
		local DataPage = DataPages:GetCurrentPage()
		print(DataPage)
		for _, Data in pairs(DataPage) do
			
			print(Data.key, Data.Value)
			
		end
		
		if DataPages.IsFinished then break else DataPages:AdvanceToNextPageAsync() end
		
	end
	
end

The only thing it prints is an empty table {} :frowning:

1 Like

To get the pages, use ListKeysAsync().

1 Like

Thanks, It’s Working Quite Good Now.
One thing though. You have to run it multiple times cause of it overloading the DataStores.
How could I fix this?

Code:

local DataStoreService = game:GetService("DataStoreService")

local Success, DataStorePages = pcall(function()
	return DataStoreService:ListDataStoresAsync()
end)
if not Success then return end


-- Gets Data Store Names:

local DataStoreNames = {}
while task.wait() do
	
	local DataStorePage = DataStorePages:GetCurrentPage()
	
	for _, DataStore in pairs(DataStorePage) do
		table.insert(DataStoreNames, DataStore.DataStoreName)
	end
	
	if DataStorePages.IsFinished then break else DataStorePages:AdvanceToNextPageAsync() end
	
end


-- Gets Data Of DataStores:

for _, DataStoreName in ipairs(DataStoreNames) do
	
	local DataStore = DataStoreService:GetDataStore(DataStoreName)
	
	local Success, Pages = pcall(function()
		return DataStore:ListKeysAsync()
	end)
	
	if not Success then warn("Timeout") continue end
	
	while task.wait() do
		
		local Items = Pages:GetCurrentPage()
		
		for _, Data in ipairs(Items) do
			
			-- Deletes Data
			
			local Key = Data.KeyName
			DataStore:RemoveAsync(Key)
			
		end
		
		if Pages.IsFinished then break else Pages:AdvanceToNextPageAsync() end
		
	end
	
end
2 Likes

Add a task.wait(3) or something.

1 Like

It Fixed The Overloading, Thank You!

1 Like

It Worked Perfectly.

Code:

local DataStoreService = game:GetService("DataStoreService")

local Success, DataStorePages = pcall(function()
	return DataStoreService:ListDataStoresAsync()
end)
if not Success then return end


-- Gets Data Store Names:

local DataStoreNames = {}
while task.wait() do
	
	local DataStorePage = DataStorePages:GetCurrentPage()
	
	for _, DataStore in pairs(DataStorePage) do
		table.insert(DataStoreNames, DataStore.DataStoreName)
	end
	
	if DataStorePages.IsFinished then break else DataStorePages:AdvanceToNextPageAsync() end
	
end

-- Gets Data Of DataStores:

for _, DataStoreName in ipairs(DataStoreNames) do
	
	local DataStore = DataStoreService:GetDataStore(DataStoreName)
	
	local Success, Pages = pcall(function()
		return DataStore:ListKeysAsync()
	end)
	
	if not Success then warn("Timeout") continue end
	
	while task.wait() do
		
		local Items = Pages:GetCurrentPage()
		
		for _, Data in ipairs(Items) do
			
			-- Deletes Data:
			local Key = Data.KeyName
			
			DataStore:RemoveAsync(Key)
			
			task.wait(3)
			
		end
		
		if Pages.IsFinished then break else Pages:AdvanceToNextPageAsync() end
		
	end
	
end
5 Likes