0
votes

I was wondering what the best way to delete my collection (which has subcollections) in firestore is. I need to delete the entire collection (using code such as this https://firebase.google.com/docs/firestore/solutions/delete-collections) every day at 20:00 UTC.

My concern is that users will be able to query/write documents to the collection/sub-collection that is being deleted. If they try to read/update/delete a document in the collection while the batch delete is running will this cause any problems?

I have thought of somehow writing some firestore rules that blocks reads if the query time is 20:00 - 20:05 UTC but it seems a bit hacky and I am not sure if it's even possible.

Could anyone provide me some assistance with how to handle potential reads at the same time as the batch delete.

Thanks a lot

Side note : In the delete collections code it mentions a token that is required functions.config().fb.token. Is this always the same If the code is running on cloud functions?

1
Hey ebg11. Did you see my answer? Did it make sese?Frank van Puffelen
Hi Frank :) I will need to think about this a bit more as I will implement changes this weekend. Thanks for the answer and the follow up, its much appreciated. If I still have issues I will add another comment.ebg11
Hi Frank, I ended up using sub collections to help control the reads... and it has simplified my deletes. However it has led to some problems with firestore rules. I have written another question here stackoverflow.com/questions/63648467/… . If you could spare some time to read it I would be grateful.ebg11

1 Answers

1
votes

There are two main scenarios I can think of here:

  1. Retry deleting the collection after the first pass, to get any documents created while your code was deleting.
  2. Block the users from writing, with a global lock in security rules.

Even if you do the second, I'd still also do the first - as it's very easy to miss a write when there are enough users.