0

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.

NicCoe
  • 409
  • 4
  • 17
  • Could you give more detail on how do you identify the docs to be removed? It is not crystal clear. – Renaud Tarnec Oct 22 '18 at 13:24
  • See Edit 2 for extra info.... This idea doesn't look very promising with Firestore, so I may utilise background services and network checks on the device. – NicCoe Oct 22 '18 at 14:46

1 Answers1

1

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:

  1. You could using some cron-style scheduling mechanism to ping a function to periodically query for and delete old documents. This implies that they may not be deleted exactly at the time you want.
  2. Use Cloud Tasks to schedule work to be done at a later time. Currently, the task will need to run on an App Engine engine instance, and may call back into Cloud Functions through another function you create in your project.

Both of these are non-trivial to set up and maintain. Read this blog for a more

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Thanks for the answer mate. I've read into the cron stuff and although can work, I really need it to be deleted at the right time as all users can see a count of how many docs are in the sub-collection, so if old docs were there it would give a false read. I can't call cron quick enough to clean it as fast as I'd like, not to mention the cost. Cloud tasks also sounds like more effort than its worth.... I might use a background service to countdown then delete the docs (incase the user exits app).. And combine maybe with cron to cleanup any missed docs every, say, 12hrs. – NicCoe Oct 22 '18 at 15:39
  • Cloud Tasks has a one-time setup cost, but after that, it's pretty low maintenance. I don't know what you mean by "background service", but that would probably get overloaded at scale, whereas Cloud Tasks should be able to handle a giant load of pending tasks. – Doug Stevenson Oct 22 '18 at 17:21
  • Oh apologies. I never mentioned I am working with Android. That may have helped. The service in the link below is run on each users phone in the background even if they exit the app. Spawning a new thread and using it to simply have a countdown then call Firestore to delete the required docs should work for most cases unless the user has no network for extended period or stops background services. That's where the cron cleanup would be handy. https://developer.android.com/guide/components/services – NicCoe Oct 22 '18 at 23:34