Deleting all documents in Firestore collection

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()
    })
}

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.


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

firebase firestore:delete --all-collections

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]