How would I delete all data from DataStores

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