We are using a cloud function to remove all data older than 6 months from our firestore. Unfortunately this ends up reaching a timeout. We have created code based on: https://firebase.google.com/docs/firestore/manage-data/delete-data
We are retrieving the collection, which we need to loop over, using listDocuments(). We can't use get() in our case, as it will not return all the documents. We have documents that have been created without explicitely creating the path towards it.
This was our first hurdle actually as the cloud function reached the timeout on that function. Updating our cloud function to the latest version (code changes) [https://github.com/googleapis/nodejs-firestore/issues/825] & increasing the timeout to 300 seconds managed to resolve the problem.
We are however now reaching timeouts on the deletion actions. We have noticed that deletions are really slow on large collections, for instance trying to delete 10 documents on a collection of 2000 documents is slower than deleting 200 documents from a collection of 210 documents. Each of these deletions can take from a few milliseconds (for small collections) to almost 3 seconds (for large collections). Because batch actions are limited to a max. of 500 [https://firebase.google.com/docs/firestore/manage-data/transactions], we end up getting multiple 3 second deletions eventually reaching the timeout.
Steps we have taken to solve the problem:
- Look over firebase documentation again & check stackoverflow for possible solutions, although there doesn't seem to be a post having a solution to this
- Contact firebase, but they weren't any help as they mentioned that it was out of their support scope and that we needed to check stackoverflow.
- Updated the cloud function to use the latest versions (& code changes), which fixed the timeout on
listDocuments() - Increased the timeout to the 540 seconds, which is the maximum we could set it on [https://cloud.google.com/functions/docs/concepts/exec#timeout]
get()- Maybe need to move from cloud functions to a longer running process if you're experiencing timeouts? - razki