Reputation: 419
I have seen a similar question with deleting Firebase Realtime Database data after a certain time, based on a write trigger here
Delete firebase data older than 2 hours
How can I achieve this in Firebase Firestore?
Info:
My Firestore layout is:
Main Collection
--Doc 1
------Sub Collection
----------Doc xyz1
----------Doc xyz2
--Doc 2
------Sub Collection
----------Doc abc1
----------Doc tyu1
----------Doc tyu2
Each user may have multiple Sub Docs (xyz1, xyz2), and they could change which Main Doc it's loaded into. Both collections are known and won't change, but the main doc and sub docs are variable. The function should wait a variable amount of time (10-120 minutes) before removing specific Sub Docs. When complete, the user/owner of the deleted Sub Docs needs a notification.
Edit: Best I can find is there are no Time To Live (TTL) documents for Firestore which I would be able to make work, and periodic checking/cleaning that is advised just doesn't work for my specific use case.
Edit 2 -Further Detail-: Each user will privately hold their unique doc/s (abc1). At their discretion, they will pick a Main doc (Doc 2 in the above), and commit their doc to its sub-collection.
At this current point in time, when the users private doc is successfully submitted to the public sub-collection, an aysnc task has a countdown timer, on completion of which, will then remove the users aforementioned document. This works great, but when the user leaves the app or doesn't have internet connection when the timer is up, the docs aren't deleted.
This leads me to the original question, thinking I could utilise Cloud Functions to perform the removal actions after the timer count-down.
Upvotes: 0
Views: 2179
Reputation: 317497
Firestore doesn't have a TTL mechanism. And Cloud Functions doesn't have a scheduling mechanism. You will have to find another way to schedule some work that will wipe out old documents.
You have a couple of options to investigate:
Both of these are non-trivial to set up and maintain. Read this blog for a more
Upvotes: 1