How would I delete all data from DataStores

you could “Delete” data by setting the Data as nil, but that would be a weird way of doing it

Isn’t :RemoveAsync() meant just for that?

Thats not deleting it. Thats just setting all the data to nil. I wish there was a way to actually delete it

People really dont understand text. “Delete”, Quotation Mark

I didn’t really see that. Sorry, my bad, es tut uns leid, perdón, scusa

If you wish to remove a key from a DataStore then I would have to side with @PahcaPower on this one as I’ve done my own research and haven’t found a way to do it. However, setting every key’s data to nil is practically deleting it. But if you are dead focused on deleting the keys, switching to a new DataStore is a better option.

Not Remove a Key, Remove Data in a Key, I know its impossible to remove the DataStore.

Then the script I provided should do the job.

You can do this by iterating through the versions of the keys and deleting them one by one. This will not only set the key to nil, but it will also delete the history of the key, the history Roblox keeps for 30 days.

Not sure if this suits your needs, but a cheap, cheat way to do it is to just delete the leaderstats / other folders from under the player when they join (PlayerAdded). this essentially means you’ve deleted the values that are communicated from the datastore, but it’s not really the same as actually deleting one.

I dont think you understand how DataStores work.

I do, hence why I said “cheap, cheat”. It doesn’t really delete the datastore, so I understand where you’re coming from, but it technically removes your access to it. I don’t really know how to word it…

Im not using leaderstats btw

hence why i also said “other folders”.

Not using Folders either

How are you storing the values then? Just putting them straight under the player?

Im using a Table to store Players Session Data under a ModuleScript.

Right now im not able to try out anything so ill get back to you when i can.

OK.

charscharschars

This text will be hidden

pfr.Str:UpdateAsync(player, “Text”) – How would I delete this?
pfr.Str:DeleteAsync(player) – How would I delete this?

You can delete an entire DataStore’s contents by calling DataStore:RemoveAsync on the key of every entry.
There is no way to return a list of DataStores. You need to manually keep track of them.

There is. I have a code snippet that will loop through all keys in a data store and delete them:

function clearData(datastore)

	local listSuccess, pages = pcall(function()
		return datastore:ListKeysAsync()
	end)
	if listSuccess then
		local keys = {}
		local pagenum = 0
		while true do
			--print(pagenum)
			local items = pages:GetCurrentPage()

			for key, info in pairs(items) do
				table.insert(keys, info.KeyName)
			end
			if pages.IsFinished then break end
			local s,e = pcall(pages.AdvanceToNextPageAsync, pages)
			--if not s then warn("Deletion pages stopped, moving on.") break end
			if s then
				pagenum+=1
			else
				print("abort!")
				break
			end
			task.wait(1)
		end
		
		for _, key in pairs(keys) do
			local s, verpages = pcall(function()
				--print("Getting versions")
				return datastore:ListVersionsAsync(key, nil, nil, DateTime.now().UnixTimestampMillis - 2592000000)
			end)
			if s then
				while true do
					local items = verpages:GetCurrentPage()

					for dkey, info in pairs(items) do
						--local cts = tostring(info.CreatedTime)
						--local realCreationTime = tonumber(cts:sub(1, cts:len()-3))
						local realCreationTime = info.CreatedTime / 1000
						--print("Key:", dkey, "; Version:", info.Version, "; Created:", os.date("%c", realCreationTime), "; Deleted:", info.IsDeleted)
						if not info.IsDeleted and realCreationTime < os.time()-2592000 then
							print(key, datastore:GetAsync(key))
							warn("The key will be deleted.")
							datastore:RemoveVersionAsync(key, info.Version)
						end
					end
					if verpages.IsFinished then break end
					verpages:AdvanceToNextPageAsync()
				end
				task.wait(2)
			else
				--print(verpages)
				if verpages:lower():match("later") then
					print("Trying again later")
					task.wait(15)
				end
			end
		end
	else
		print(pages)
	end
end

I copied this from SSecurity’s source, which is used to clear user deletion requests automatically in the background after 30 days.

Please note that it should delete the version history of the key as well, so if there is any sensitive information that you want to recover within 30 days it cannot be obtained.

2 Likes