Deleting all documents in Firestore collection

SwiftGoogle Cloud-Firestore

Swift Problem Overview


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

Swift Solutions


Solution 1 - Swift

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

Solution 2 - Swift

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

firebase firestore:delete --all-collections

Solution 3 - Swift

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.

Solution 4 - Swift

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]

Solution 5 - Swift

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

Solution 6 - Swift

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.

Solution 7 - Swift

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

Solution 8 - Swift

this worked for me by THEODORE above.

db.collection("collectionName")
  .get()
  .then(res => {
    res.forEach(element => {
      element.ref.delete();
    });
  });

i dont have the reputaiton to reply directly to his comment. but in addition to his solution if you need to delete a sub-collection using this method just do this.

db.collection(`collectionName/docID/subcollection`) //make sure to use backtics
  .get()
  .then(res => {
    res.forEach(element => {
      element.ref.delete();
    });
  });

if the docID is auto generated you can use this method below. which is what i was using it for to delete notificaitons for a user when they click the clear all button.

db.collection(`collectionName/${variable}/subcollection`) 
        .get()
        .then((res) => {
          res.forEach((element) => {
            element.ref.delete();
          });
        });

the variable can be whatever you're setting the docID with. in my instance it was the user.uid

Solution 9 - Swift

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;
	}
}

Solution 10 - Swift

db.collection("collectionName")
  .get()
  .then(res => {
    res.forEach(element => {
      element.ref.delete();
    });
  });

Solution 11 - Swift

This is the approach that I took. While it works fine, I'm not sure what other hidden issues it might have.

function deleteCollection(collectionPath, batchSize=400){
    
    let deletePromise = appFirestore.collection(collectionPath).listDocuments()
                    .then( function(docs) {

                        let batch = appFirestore.batch();

                        if(docs.length <= batchSize){
                            docs.map( (doc) => {
                                batch.delete(doc);
                            });
                            batch.commit();
                            return true;
                        }
                        else{
                            for (let i = 0; i < batchSize; i++){
                                batch.delete(docs[i]);
                            }
                            batch.commit();
                            return false;
                        }
                    })
                    .then( function(batchStatus) {
                        return batchStatus ? true : deleteCollection(collectionPath, batchSize, debug);
                    })
                    .catch( function(error) {
                        console.error(`Error clearing collections (${error})`);
                        return false;
                    });

    return deletePromise;
}

Solution 12 - Swift

listDocuments works only in firebase-admin:

async function deleteCollection(path: string): Promise<FirebaseFirestore.WriteResult[]> {

const batch = firestore.batch();
const documentsInCollection = await firestore.collection(path).listDocuments();
documentsInCollection.map((doc) => batch.delete(doc));

return batch.commit();
};

Solution 13 - Swift

There is not a simple way to do this through the API.

To delete multiple documents at once efficiently:

  1. Perform a one-time read of the documents in the collection.
  2. You can use a where clause to limit which documents you retrieve.
  3. Create a write batch.
  4. Queue all of the retrieved documents up for deleting in the batch.
  5. Commit the batch to start deleting documents.
  6. Add appropriate error handlers to listen for errors with reading and deleting documents.

Shown below is an example of how to do this with Android Java.

public void deleteAllMyThings() {
  db.collection("userThings")
    .whereEqualTo("userId", userId)
    .get()
    .addOnSuccessListener((querySnapshot) -> {
      WriteBatch batch = db.batch();
      for (QueryDocumentSnapshot doc : querySnapshot) {
        batch.delete(doc.getReference());
      }

      batch
        .commit()
        .addOnSuccessListener((result) -> {
           Log.i(LOG_TAG, "All my things have been deleted.");
        })
        .addOnFailureListener((error) -> {
          Log.e(LOG_TAG, "Failed to delete all my things.", error);
        });
      })
      .addOnFailureListener((error) -> {
        Log.e(LOG_TAG, "Failed to get all my things.", error);
      });
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJakeView Question on Stackoverflow
Solution 1 - SwiftAron GatesView Answer on Stackoverflow
Solution 2 - SwiftKebabmanView Answer on Stackoverflow
Solution 3 - SwiftFrank van PuffelenView Answer on Stackoverflow
Solution 4 - SwiftTilak MaddyView Answer on Stackoverflow
Solution 5 - SwiftAlex MckayView Answer on Stackoverflow
Solution 6 - Swiftg2serverView Answer on Stackoverflow
Solution 7 - SwiftQueenie PengView Answer on Stackoverflow
Solution 8 - SwiftStrid3r21View Answer on Stackoverflow
Solution 9 - Swiftdelavago1999View Answer on Stackoverflow
Solution 10 - SwiftTHEODOREView Answer on Stackoverflow
Solution 11 - SwiftbuginetteeView Answer on Stackoverflow
Solution 12 - SwiftLorkaView Answer on Stackoverflow
Solution 13 - SwiftPaul SmithView Answer on Stackoverflow