Delete documents with dates older than x in collection from a set of documents in firebase
If you want to delete outdated history across all users, you'll want to use a collection group query. This allows you to query the history
collections across all users in one go.
Aside from that, the code would be quite simple, starting with a query on the time
field:
const now = Date.now();
const lastMonth = now - 30*24*60*60*1000;
const historiesRef = db.collectionGroup('history');
const expiredDocsSnapshot = await historiesRef.where('time', '<', lastMonth).get();
And then you loop over the results, deleting them one-by-one or in batches.
You may want to put a limit
on the query to reduce the number of documents you process in one go, although if that is needed you could also increase how frequently you run the cleanup process.
Deleting Data from Firebase Periodically
Solution 1 - Cloud Functions
Login to firebase and then select your app, you will now be on the App Overview page. Select the Cloud Function Item. This will allow you to execute some javaScript based on a trigger, there are many different types of triggers such as database triggers or authentication triggers. In this case, you want the trigger to be based on time.
That is this one:
Firebase explains cloud functions better than I can below:
After you write and deploy a function, Google’s servers begin to manage the function immediately, listening for events and running the function when it is triggered. As the load increases or decreases, Google responds by rapidly scaling the number of virtual server instances needed to run your function.
Lifecycle of a function
The developer writes code for a new function, selecting an event provider (such as Realtime Database), and defining the conditions under which the function should execute.
The developer deploys the function, and Firebase connects it to the selected event provider.
When the event provider generates an event that matches the function's conditions, the code is invoked.
If the function is busy handling many events, Google creates more instances to handle work faster. If the function is idle, instances are cleaned up.
When the developer updates the function by deploying updated code, all instances for the old version are cleaned up and replaced by new instances.
When a developer deletes the function, all instances are cleaned up, and the connection between the function and the event provider is removed.
source: https://firebase.google.com/docs/functions/
Solution 2 - Server + Cron Jobs (my preference)
Purchase a cheap server on a monthly subscription (don't need anything fancy should be a few dollars a month), will struggle to find a free one that allows Cron jobs though.
write a script that connects to the firebase database and deletes all the posts older than an hour.
on your server dashboard/control panel create a new Cron Job scheduled to run every hour and point it to the script you just wrote.
How to create an auto delete mechanism for firestore? (deleting data after time period)
There is no built-in time-to-live mechanism in Cloud Firestore. The common approach is to run a piece of code at an interval, e.g. a Cloud Function triggered by something like cron-job.org.
Have a look at these questions for samples:
- Delete firebase data older than 2 hours
- How to delete firebase data after "n" days
- Impelementing aging in a Firebase real time database
- How to schedule a Cloud Functions to run in the future in order to build a Firestore document TTL
While these are for the Firebase Realtime Database, the same approach applies to Cloud Firestore.
Related Topics
Sleep in JavaScript - Delay Between Actions
How Does This Object Method Definition Work Without the "Function" Keyword
What Are Alternatives to Document.Write
Read :Hover Pseudo Class with JavaScript
How to Check If an External (Cross-Domain) CSS File Is Loaded Using JavaScript
Manipulating CSS with JavaScript
Problem Deploying Rails 3.1 Project to Heroku: Could Not Find a JavaScript Runtime
Why Does the Setinterval Callback Execute Only Once
Any Difference Between Await Promise.All() and Multiple Await
JavaScript Function to Add X Months to a Date
How to Use Backslashes (\) in a String
How to Get the Difference Between Two Dates in JavaScript
Black and White Text Based on Background Image with CSS
How to Detect CSS3 Resize Events
What Chars Needs Escaping in Queryselector
Span Inside Button, Is Not Clickable in Firefox
Rails: Post 422 (Unprocessable Entity) in Rails? Due to the Routes or the Controller