ebg11
ebg11

Reputation: 1046

Firestore Batch delete while reading document

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?

Upvotes: 0

Views: 188

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

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.

Upvotes: 1

Related Questions