Deleting a DataStore key after a period of time

Hi! I’m new around here, so sorry if this question sounds kinda stupid. I was wondering if it would be possible to delete a DataStore key after a period of time, but a long one (3-7 days). One way I could think of is by deleting the key and accessing the tombstone version of that key while it’s available, but it’s not quite an elegant solution and doesn’t allow us to choose when to delete. Any ideas?

Well since your time is varying, what are your calculations based on?

I am making a “replay match” system for my game where a player can have certain “replay codes” if they choose to, that will save a match they just played to a DataStore so that the user (or their friends) can use that replay code to see how a match went. Obviously that will take up a lot of memory, so what I want to do is make these codes expire (get deleted) a fixed amount of days after they were created.

Alright if you’re not sure what time period then you can just use a math.random() statement to calculate how many days.

Then you can use some funky os.clock() stuff to wait those days and delete the key after that!

Well, I know how to find out how many days to wait until the key should be deleted. But how should the key be deleted?

  1. Using wait() would be very nonsensical as the server would likely shut down by the time the wait is finished.
  2. Deleting the key the next time that player joins the game would make sense, but there’s no guarantee that the player will rejoin ever again.
  3. Looping through all available keys, especially if there’s a ton of them, would require a lot of processing and networking power.

You actually can’t delete your datastore key, you can just set everything to nil.
It will eternally be available though.

1 Like

As per the GlobalDataStore documentation, calling RemoveAsync on a key will set it to nil (essentially just deleting it), but will still be available for 30 days when calling GetVersionAsync. Thanks for the info though

My bad then, I misread the datastore documentation.

In that case, you should store all of your “to be deleted datastores” inside of a datastore with a time for them to be destroyed.

Every x increment of time, you check in to see if the time has been passed for each of your “to be deleted datastores”. If you had a lot of em you could divide them into categories based on if they were due in a day, week, month etc and update them every once in awhile.

Now that you know if your datastore is due to be deleted you use your RemoveAsync function.

Note:
I have never used datastores, this is just what i would do.

1 Like

So basically I can store all the datastore keys that are due deletion, together with the time to delete, in a separate datastore, and every now and then check if anything expired?

I think that is the best way to do so.

1 Like

Thanks for the advice, I’ll give that a try!

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