With Firebase real time database we can delete a huge list of items with one single command simply by calling remove ()
on the parent node (the node is deleted and all is children too).
But according to the documentation with Firestore (https://firebase.google.com/docs/firestore/manage-data/delete-data#collections ) :
to delete a Collection we have to code a batch that will to loop over all its documents and delete them one by one .
This is not efficient at all. Is it because Firestore is in beta version or is it structurally impossible to delete the full node (Collection) in one single call ?
To delete an entire collection or subcollection in Cloud Firestore, retrieve all the documents within the collection or subcollection and delete them. If you have larger collections, you may want to delete the documents in smaller batches to avoid out-of-memory errors.
To delete multiple documents, you can do a single batched write. The WriteBatch class has a delete() method for this purpose. The performance to between a single BatchedWrite and multiple DocumentReference.
A collection contains documents and nothing else. It can't directly contain raw fields with values, and it can't contain other collections. (See Hierarchical Data for an explanation of how to structure more complex data in Cloud Firestore.) The names of documents within a collection are unique.
The RTDB is able to do this because each database is local to a single region. In order to provide a serialized view, when you call remove()
, the database stops all other work until the removal is complete.
This behavior has been the cause of several apparent outages: if a remove()
call has to delete huge swaths of data, all other activity is effectively locked out until it completes. As a result even for RTDB users that want to delete large quantities of data we have recommended recursively finding and deleting documents in groups (CLI, node.js).
Firestore on the other hand is based on more traditional Google-style storage infrastructure where different ranges of keys are assigned dynamically to different servers (storage isn't actually backed by BigTable, but the same principles apply). This means that deleting data is no longer a necessarily a single region action and it becomes very expensive to effectively make the deletion appear transactional. Firestore transactions are currently limited to 100 participants and this means that any non-trivial transactional bulk deletion is impossible.
We're investigating how best to surface an API that does a bulk deletion without promising transactional behavior. It's straightforward to imagine how to do this from a mobile client, but as you've observed this wouldn't be efficient if all we did is embedded the loop and batch delete for you. We also don't want to make REST clients second-class citizens either.
Firestore is a new product and there are ton of things still to do. Unfortunately this just hasn't made the cut. While this is something we hope to address eventually I can't provide any timeline on when that would be.
In the meantime the console and the firebase command-line both provide a non-transactional means of doing this, e.g. for test automation.
Thanks for your understanding and thanks for trying Firestore!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With