Is there any TTL option on documents for Firebase Firestore . Where documents get auto deleted after that amount time
Asked
Active
Viewed 1.1k times
1 Answers
18
There is no such built-in functionality.
The easiest way to build it yourself is by:
- Adding a
expirationTimestampproperty to your documents. Denying read of documents whose expiration has passed in your security rules.
match /collection/{document} { allow read: if resource.data.expirationTimestamp > request.time.date(); }Unfortunately this means that you won't be able to query the collection anymore. You'll need to access the individual documents.
Periodically run Cloud Functions code to delete expired documents.
Also see Doug's excellent blog post describing this process: How to schedule a Cloud Function to run in the future with Cloud Tasks (to build a Firestore document TTL).
Frank van Puffelen
- 499,950
- 69
- 739
- 734
-
2Also, you can now use Cloud Tasks to schedule a function that performs the future deletion of a document. – Doug Stevenson Jun 01 '19 at 20:16
-
Cloud Tasks is need payment to use? – luke cross Jun 12 '20 at 19:29
-
1https://cloud.google.com/tasks#pricing "First 1 Million operations per month are free" – Frank van Puffelen Jun 12 '20 at 20:16