I am trying to clear a Firestore database which was filled with a lot of documents and subcollections for testing purposes. The Firebase CLI ([email protected]) suggests the following possibility to delete data from Cloud Firestore:
Usage: firestore:delete [options] [path]
Options:
-r, --recursive Recursive. Delete all documents and subcollections. Any action which would result in the deletion of child documents will fail if this argument is not passed. May not be passed along with --shallow.
--shallow Shallow. Delete only parent documents and ignore documents in subcollections. Any action which would orphan documents will fail if this argument is not passed. May not be passed along with -r.
--all-collections Delete all. Deletes the entire Firestore database, including all collections and documents. Any other flags or arguments will be ignored.
-y, --yes No confirmation. Otherwise, a confirmation prompt will appear.
The problem is that it does not really work for me.
Executing firebase firestore:delete --all-collections
produces the following output:
You are about to delete YOUR ENTIRE DATABASE. Are you sure? Yes
Deleting the following collections: 13OPlWrRit5PoaAbM0Rk, 17lHmJpTKVn1MBBbC169, 18LvlhhaCA1tygJYqIDt, 1DgDspzJwSEZrYxeM5G6, 1GQE7ySki4MhXxAeAzpx, 1MhoDe5JZY8Lz3yd7rVl, 1NOZ7OJeqSKl38dyh5Sw, 1Rxkjpgmr3gKvYhBJX29, 1S3mAhzQMd137Eli7qAp, 1S8FZxuefpIWBGx0hJW2, 1a7viEplYa79eNNus5xC, 1cgzMxAayzSkZv2iZf6e, 1dGjESrw6j12hEOqMpky, 1dbfgFD5teTXvQ6Ym897, 1eeYQgv2BJIS0aFWPksD, 1ehWNAZ0uKwg7mPXt3go, 1fDTkbwrXmGwZlFUl3zi, 1k5bk4aiMCuPw2KvCoAl, 1pxUSDh1YqkQAcuUH9Ie, 1rMSZ5Ru0cAfdcjY0Ljy
Deleted 92 docs (652 docs/s)
Even after executing the function multiple times an awful lot of documents and subcollections still remain in the Firestore database. Instead of deleting the ENTIRE DATABASE
, only about 70-150 documents are deleted when the command is executed.
How can the entire database be deleted?
Call the doc() method and pass references of database, collection name and ID of a document that we want to delete. Assign it to a constant called docRef. Let's make a deleteDoc() query to perform deleting a document from a cities collection in Cloud Firestore.
To delete multiple documents, you can do a single batched write. The WriteBatch class has a delete() method for this purpose.
I've reported this as a bug and received the following answer:
Currently, this is an intended behavior. As stated in our documentation, deleting a collection of more than 500 documents requires multiple batched operations. So doing the iteration would be a good way to handle cases of partial deletion. I would also suggest that you check our docs regarding some of the callable function limitations for more details.
This means that firebase-tools always deletes a maximum of 500 documents in one operation. My solution to delete all collections and documents in the database is to use a while loop:
while firebase firestore:delete --all-collections --project MYPROJECT -y; do :; done
After some iterations you will see that there are no collections left and you can stop the script. Your Firestore DB is now completely empty.
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