I would like to delete any data that is older that two hours. Currently, on the client side, I loop through all the data, and run a delete on any that is older. When I do this, the db.on('value') function is invoked every time something is deleted. Also, things will only be deleted when a client connects, and what might happen if two clients connect at once?
Where can I set up something that deletes old data? I have a time stamp inside each object created by a JavaScript Date.now().
I have a http triggered cloud function that deletes nodes, depending on when they were created and their expiration date.
When I add a node to the database, it needs two fields: timestamp to know when it was created, and duration to know when the offer must expire.
Then, I have this http triggered cloud function:
You can create a cron job that every X minutes makes a request to the URL of that function: https://cron-job.org/en/
But I prefer to run my own script, that makes a request every 10 seconds:
In the latest version of Firebase API, ref() is changed to ref
You could look into Scheduling Firebase Functions with Cron Jobs. That link shows you how to schedule a Firebase Cloud Function to run at a fixed rate. In the scheduled Firebase Function you could use the other answers in this thread to query for old data and remove it.
Firebase does not support queries with a dynamic parameter, such as "two hours ago". It can however execute a query for a specific value, such as "after August 14 2015, 7:27:32 AM".
That means that you can run a snippet of code periodically to clean up items that are older than 2 hours at that time:
As you'll note I use
child_added
instead ofvalue
, and IlimitToLast(1)
. As I delete each child, Firebase will fire achild_added
for the new "last" item until there are no more items after the cutoff point.Update: if you want to run this code in Cloud Functions for Firebase:
This function triggers whenever data is written under
/path/to/items
, so child nodes will only be deleted when data is being modified.This code is now also available in the
functions-samples
repo.