Can't get DatastoreV2 to remove keys

Hello!

I’m trying to remove keys of people who’ve joined my game but aren’t friends, however I can’t get it to work for some reason.

Also excuse the horrible variables and not using :GetService, I’m running it in the command line so I don’t need to be fancy or anything. I’m also using DatastoreV2 so that might be contributing to the issue, I really don’t know though.

I was also able to set the data to a blank table, but not actually remove it.

local o = Instance.new('DataStoreOptions') 
o:SetExperimentalFeatures({v2 = true}) 
local ds: DataStore = game.DataStoreService:GetDataStore('PLAYERDATA', nil, o)
local keys = ds:ListKeysAsync(nil, 50) 

local f = game.Players:GetFriendsAsync(605492589)

local fs = {}

while true do
	for i, v in pairs(f:GetCurrentPage()) do
		table.insert(fs, v.Id)
	end
	if not f.IsFinished then
		f:AdvanceToNextPageAsync()
	else
		break
	end
end
print(fs)

while true do
	for i, v:DataStoreKey in pairs(keys:GetCurrentPage()) do
		if not table.find(fs, tonumber(v.KeyName)) and v.KeyName ~= '605492589' then
			print('rm',v.KeyName,ds:RemoveAsync(v.KeyName)) -- no errors
			task.wait()
		end
	end
	if not keys.IsFinished then
		keys:AdvanceToNextPageAsync()
	else
		break
	end
end

wait(10)

local keys = ds:ListKeysAsync(nil, 50)

while true do
	for i, v:DataStoreKey in pairs(keys:GetCurrentPage()) do
		print(v.KeyName) -- still prints the keys that should have been removed
	end
	if not keys.IsFinished then
		keys:AdvanceToNextPageAsync()
	else
		break
	end
end

forgive me if I’m wrong but shouldn’t the : need to be an = or maybe be connected to DataStore ?

That’s just a typechecking thing, it’s basically the same as doing local ds = game.DataStoreService:GetDataStore('PLAYERDATA', nil, o) but it has better autocomplete with the new beta.

1 Like

Just checking but is nil assigned to any of those keys, most specifically the latest version of the key? You can also check if GetAsync returns nil for that key. To my knowledge RemoveAsync no longer hard removes data but creates a “tombstone” version for the key so that the latest one returns nil but previous versions remain accessible for restoration purposes.

Make yourself a reminder for development to check the keys in 30 days, they should no longer exist assuming no new data has been set for that key. You can use a dummy account to test - for example, assign data to the Roblox account UserId (1) and then remove it, then check in 30 days to see if said set keys still exist for the Roblox account.

1 Like

Yeah, the data wasn’t removed at all or set to nil, nor was a new version/a tombstone version created. The only way I was able to remove the data was through setting their data to a blank table which isn’t optimal but I suppose it works. I fear it will be an issue if I get any erasure requests though.