5

I am trying to clear a Firestore database which was filled with a lot of documents and subcollections for testing purposes. The Firebase CLI (firebase-tools@3.18.4) 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?

jmeinke
  • 530
  • 6
  • 27
  • If the CLI is obviously not doing what you expect, please file a bug report: https://firebase.google.com/support/contact/bugs-features/ – Doug Stevenson May 31 '18 at 06:46

2 Answers2

1

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.

jmeinke
  • 530
  • 6
  • 27
  • Have you tried to `.set()` with an empty object? I know that works to delete the entire db in realtime db. – Ronnie Royston Feb 24 '19 at 17:25
  • Hey @RonRoyston, no this does not work. In Firestore, each document might have subcollections - even if the document is deleted, the subcollections still exist. See https://firebase.google.com/docs/firestore/manage-data/delete-data#delete_documents – jmeinke Feb 24 '19 at 23:25
0

You will want to use the admin sdk for this task. Use .listDocuments and .listCollections to build out simple iterations to perform your .delete operation(s) against.

If a documents .listCollections response is zero in length or null or empty, you know there's no subcollections and can iterate on / skip. Else, iterate that subcollections documents looking for deeper subcollections to delete.

let documentRef = firestore.doc('col/doc');

documentRef.listCollections().then(collections => {
  for (let collection of collections) {
    console.log(`Found subcollection with id: ${collection.id}`);
  }
});

and

let collectionRef = firestore.collection('col');

return collectionRef.listDocuments().then(documentRefs => {
   return firestore.getAll(documentRefs);
}).then(documentSnapshots => {
   for (let documentSnapshot of documentSnapshots) {
      if (documentSnapshot.exists) {
        console.log(`Found document with data: ${documentSnapshot.id}`);
      } else {
        console.log(`Found missing document: ${documentSnapshot.id}`);
      }
   }
});
Ronnie Royston
  • 16,778
  • 6
  • 77
  • 91