Data Store Support

Hello!

I was wondering if there was a way to delete all of the data stores created under my game.

Let me know if you have ideas

You can’t directly delete an actual datastore,

once called and created via :GetDataStore("DataStoreName") it cannot be deleted by a script. You can remove all the data from it, and replace DataStoreName with the name of another datastore to create a new one.

If you do want to remove the data from All Datastores, you can run this. I believe it should also work in cmd line

local DataStoreService = game:GetService("DataStoreService")

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

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

for _, DataStoreName in ipairs(DataStores) 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			
			local Key = Data.KeyName
			DataStore:RemoveAsync(Key)
			
		end
		
		if Pages.IsFinished then break else Pages:AdvanceToNextPageAsync() end
		
	end
	
end

Adapted from:

Note, this will not delete the datastore, that’s not possible, but it’ll remove the data from all datastores

1 Like

To build off this, there’s no convenience method for wiping all the keys in a datastore either. You have to do something like this:

function deleteAllKeys(datastore)
  local pages = datastore:ListKeysAsync()
  repeat
    for key, _ in pairs(pages:GetCurrentPage()) do
      datastore:RemoveAsync(key)
    end
    pages:AdvanceToNextPageAsync()
  until pages.IsFinished
end

e: looks like he beat me to the punch lol

2 Likes

@tDaymon @thyswedishviking

Thank you both

No worries, glad I could help. Have a blessed rest of your day man! God bless :wave:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.