function deleteCollection(db, collectionPath, batchSize) {
var collectionRef = db.collection(collectionPath);
var query = collectionRef.orderBy('__name__').limit(batchSize);
return new Promise((resolve, reject) => {
deleteQueryBatch(db, query, batchSize, resolve, reject);
function deleteQueryBatch(db, query, batchSize, resolve, reject) {
// When there are no documents left, we are done
if (snapshot.size == 0) {
// Delete documents in a batch
snapshot.docs.forEach(doc => {
return batch.commit().then(() => {
// Recurse on the next process tick, to avoid
deleteQueryBatch(db, query, batchSize, resolve, reject);