42
votes

I'm looking for a way to clear an entire collection. I saw that there is a batch update option, but that would require me to know all of the document IDs in the collection.

I'm looking for a way to simply delete every document in the collection.

Thanks!

Edit: Answer below is correct, I used the following:

  func delete(collection: CollectionReference, batchSize: Int = 100) {
    // Limit query to avoid out-of-memory errors on large collections.
    // When deleting a collection guaranteed to fit in memory, batching can be avoided entirely.
    collection.limit(to: batchSize).getDocuments { (docset, error) in
      // An error occurred.
      let docset = docset

      let batch = collection.firestore.batch()
      docset?.documents.forEach { batch.deleteDocument($0.reference) }

      batch.commit {_ in
        self.delete(collection: collection, batchSize: batchSize)
      }
    }
  }
8

8 Answers

17
votes

There is no API to delete an entire collection (or its contents) in one go.

From the Firestore documentation:

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. Repeat the process until you've deleted the entire collection or subcollection.

There is even a Swift sample in that documentation, so I recommend you try it.

The Firebase CLI allows you to delete an entire collection with a single command, but it just calls the API to delete all documents in that collection in batches. If this suits your needs, I recommend you check out the (sparse) documentation for the firestore:delete command.

32
votes

There is now an option in the firebase CLI to delete an entire firestore database:

firebase firestore:delete --all-collections
30
votes

The following javascript function will delete any collection:

deleteCollection(path) {
    firebase.firestore().collection(path).listDocuments().then(val => {
        val.map((val) => {
            val.delete()
        })
    })
}

This works by iterating through every document and deleting each.

Alternatively, you can make use of Firestore's batch commands and delete all at once using the following function:

deleteCollection(path) {
    // Get a new write batch
    var batch = firebase.firestore().batch()

    firebase.firestore().collection(path).listDocuments().then(val => {
        val.map((val) => {
            batch.delete(val)
        })

        batch.commit()
    })
}
11
votes

2020 updated answer

You can do it with Node JS - (notice they used process which is a famous object in node not available in Web javascript)

Look at this snippet on Github hosted by firebase. I always had that page pinned to my browser ;)

// [START delete_collection]

async function deleteCollection(db, collectionPath, batchSize) {
  const collectionRef = db.collection(collectionPath);
  const query = collectionRef.orderBy('__name__').limit(batchSize);

  return new Promise((resolve, reject) => {
    deleteQueryBatch(db, query, resolve).catch(reject);
  });
}

async function deleteQueryBatch(db, query, resolve) {
  const snapshot = await query.get();

  const batchSize = snapshot.size;
  if (batchSize === 0) {
    // When there are no documents left, we are done
    resolve();
    return;
  }

  // Delete documents in a batch
  const batch = db.batch();
  snapshot.docs.forEach((doc) => {
    batch.delete(doc.ref);
  });
  await batch.commit();

  // Recurse on the next process tick, to avoid
  // exploding the stack.
  process.nextTick(() => {
    deleteQueryBatch(db, query, resolve);
  });
}

// [END delete_collection]
5
votes

The cleanest way I have found to delete all documents. The only time I would use this function is when using the emulator and you can simply paste the function into the console:

// Paste this in:
function clearCollection(path) {
  const ref = firestore.collection(path)
  ref.onSnapshot((snapshot) => {
    snapshot.docs.forEach((doc) => {
      ref.doc(doc.id).delete()
    })
  })
}
// Use it like this:
clearCollection('layers')

If you find yourself needing this code repeatedly save it as a snippet in Chrome and then you can have easy access to it and won't have to keep pasting the code block into the console. You must run the snippet before it is accessible from the code block. Documentation

2
votes

Tested in VueJS

import db from '@/firebase/init' 

let ref = db.collection('YOUR_COLLECTION_NAME')

db.collection(path).onSnapshot(snapshot => {
    snapshot.docs.forEach(doc => {
        ref.doc(doc.id).delete()
        .catch(error => {
            console.log(error)
        })
    })
})

1
votes

versions from v4.10.0 can now bulk delete using this method.

await firestore.recursiveDelete(firestore.collection('foo'));

It uses BulkWriter to perform the deletes.

0
votes

You have to get all the documents then use batch to delete them in bulk P.S. i prefer try...catch syntax

    let deleteInBatch = async (query, size = 100) => {
    try{

        let batch = firestore().batch();

        //get documents
        let values = await query.get();
        if(values.size>0){
            values.foreach(value=> {
                batch.delete(value.ref);
            })

            //Delete the documents in bulk
            batch.commit();
            if(values.size>0){
                //Recusively call the function again to finish
                //deleting the rest of documents
                deleteInBatch(query,size);
            }
        }else{
            //exist function
            return;
        }
    }catch(err){
        throw err;
    }
}