// asynchronously delete a documentApiFuture<WriteResult>writeResult=db.collection("cities").document("DC").delete();// ...System.out.println("Update time : "+writeResult.get().getUpdateTime());
import("context""log""cloud.google.com/go/firestore")funcdeleteDoc(ctxcontext.Context,client*firestore.Client)error{_,err:=client.Collection("cities").Doc("DC").Delete(ctx)iferr!=nil{// Handle any errors in an appropriate way, such as returning them.log.Printf("An error has occurred: %s",err)}returnerr}
When you delete a document, Firestore does not automatically
delete the documents within its
subcollections. You can still access the subcollection documents by reference.
For example, you can access the document at path/mycoll/mydoc/mysubcoll/mysubdoceven
if you delete the ancestor document at/mycoll/mydoc.
Non-existent ancestor documentsappear in the console,
but they do not appear in query results and snapshots.
If you want to delete a document and all the documents within its
subcollections, you must do so manually. For more information, seeDelete Collections.
Delete fields
To delete specific fields from a document, use the following language-specificFieldValue.delete()methods
when you update a document:
Web version 9
Use thedeleteField()method:
import{doc,updateDoc,deleteField}from"firebase/firestore";constcityRef=doc(db,'cities','BJ');// Remove the 'capital' field from the documentawaitupdateDoc(cityRef,{capital:deleteField()});
varcityRef=db.collection('cities').doc('BJ');// Remove the 'capital' field from the documentvarremoveCapital=cityRef.update({capital:firebase.firestore.FieldValue.delete()});
valdocRef=db.collection("cities").document("BJ")// Remove the 'capital' field from the documentvalupdates=hashMapOf<String,Any>("capital"toFieldValue.delete(),)docRef.update(updates).addOnCompleteListener{}
DocumentReferencedocRef=db.collection("cities").document("BJ");// Remove the 'capital' field from the documentMap<String,Object>updates=newHashMap<>();updates.put("capital",FieldValue.delete());docRef.update(updates).addOnCompleteListener(newOnCompleteListener<Void>(){// ...// ...
finaldocRef=db.collection("cities").doc("BJ");// Remove the 'capital' field from the documentfinalupdates=<String,dynamic>{"capital":FieldValue.delete(),};docRef.update(updates);
DocumentReferencedocRef=db.collection("cities").document("BJ");Map<String,Object>updates=newHashMap<>();updates.put("capital",FieldValue.delete());// Update and delete the "capital" field in the documentApiFuture<WriteResult>writeResult=docRef.update(updates);System.out.println("Update time : "+writeResult.get());
// Create a document referenceconstcityRef=db.collection('cities').doc('BJ');// Remove the 'capital' field from the documentconstres=awaitcityRef.update({capital:FieldValue.delete()});
import("context""log""cloud.google.com/go/firestore")funcdeleteField(ctxcontext.Context,client*firestore.Client)error{_,err:=client.Collection("cities").Doc("BJ").Update(ctx,[]firestore.Update{{Path:"capital",Value:firestore.Delete,},})iferr!=nil{// Handle any errors in an appropriate way, such as returning them.log.Printf("An error has occurred: %s",err)}// ...returnerr}
To delete an entire collection or subcollection in Firestore,
retrieve (read) all the documents within the collection or subcollection and delete
them. This process incurs both read and delete costs. 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.
Deleting a collection requires coordinating an unbounded number of
individual delete requests. If you need to delete entire collections, do so only
from a trusted server environment. While it is possible to delete a collection
from a mobile/web client, doing so has negative security and performance implications.
The following snippets are simplified for clarity and don't include error
handling, security, deleting subcollections, or performance optimizations. To learn more
about one recommended approach to deleting collections in production, seeDeleting Collections and Subcollections.
Web
// Deleting collections from a Web client is not recommended.
Swift
Note:This product is not available on watchOS and App Clip targets.
// Deleting collections from an Apple client is not recommended.
Objective-C
Note:This product is not available on watchOS and App Clip targets.
// Deleting collections from an Apple client is not recommended.
Kotlin Android
// Deleting collections from an Android client is not recommended.
Java Android
// Deleting collections from an Android client is not recommended.
Dart
Deleting collections from the client is not recommended.
Java
/*** Delete a collection in batches to avoid out-of-memory errors. Batch size may be tuned based on* document size (atmost 1MB) and application requirements.*/voiddeleteCollection(CollectionReferencecollection,intbatchSize){try{// retrieve a small batch of documents to avoid out-of-memory errorsApiFuture<QuerySnapshot>future=collection.limit(batchSize).get();intdeleted=0;// future.get() blocks on document retrievalList<QueryDocumentSnapshot>documents=future.get().getDocuments();for(QueryDocumentSnapshotdocument:documents){document.getReference().delete();++deleted;}if(deleted>=batchSize){// retrieve and delete another batchdeleteCollection(collection,batchSize);}}catch(Exceptione){System.err.println("Error deleting collection : "+e.getMessage());}}
// This is not supported. Delete data using CLI as discussed below.
Node.js
asyncfunctiondeleteCollection(db,collectionPath,batchSize){constcollectionRef=db.collection(collectionPath);constquery=collectionRef.orderBy('__name__').limit(batchSize);returnnewPromise((resolve,reject)=>{deleteQueryBatch(db,query,resolve).catch(reject);});}asyncfunctiondeleteQueryBatch(db,query,resolve){constsnapshot=awaitquery.get();constbatchSize=snapshot.size;if(batchSize===0){// When there are no documents left, we are doneresolve();return;}// Delete documents in a batchconstbatch=db.batch();snapshot.docs.forEach((doc)=>{batch.delete(doc.ref);});awaitbatch.commit();// Recurse on the next process tick, to avoid// exploding the stack.process.nextTick(()=>{deleteQueryBatch(db,query,resolve);});}
import("context""fmt""io""cloud.google.com/go/firestore""google.golang.org/api/iterator")funcdeleteCollection(wio.Writer,projectID,collectionNamestring,batchSizeint)error{// Instantiate a clientctx:=context.Background()client,err:=firestore.NewClient(ctx,projectID)iferr!=nil{returnerr}col:=client.Collection(collectionName)bulkwriter:=client.BulkWriter(ctx)for{// Get a batch of documentsiter:=col.Limit(batchSize).Documents(ctx)numDeleted:=0// Iterate through the documents, adding// a delete operation for each one to the BulkWriter.for{doc,err:=iter.Next()iferr==iterator.Done{break}iferr!=nil{returnerr}bulkwriter.Delete(doc.Ref)numDeleted++}// If there are no documents to delete,// the process is over.ifnumDeleted==0{bulkwriter.End()break}bulkwriter.Flush()}fmt.Fprintf(w,"Deleted collection \"%s\"",collectionName)returnnil}
// This is not supported. Delete data using CLI as discussed below.
C#
privatestaticasyncTaskDeleteCollection(CollectionReferencecollectionReference,intbatchSize){QuerySnapshotsnapshot=awaitcollectionReference.Limit(batchSize).GetSnapshotAsync();IReadOnlyList<DocumentSnapshot>documents=snapshot.Documents;while(documents.Count>0){foreach(DocumentSnapshotdocumentindocuments){Console.WriteLine("Deleting document {0}",document.Id);awaitdocument.Reference.DeleteAsync();}snapshot=awaitcollectionReference.Limit(batchSize).GetSnapshotAsync();documents=snapshot.Documents;}Console.WriteLine("Finished deleting all documents from the collection.");}
A TTL policy designates a given field as the expiration time for documents in a
given collection group. TTL delete operations count towards your document delete
costs.
Firestore supports several tools for bulk deletion. You should
select a tool based on the number of documents you need to delete and the
level of configurability you need.
For smaller jobs
of thousands of documents, use the console or the Firebase CLI. For larger
jobs, these tools might start to timeout and require you to run the tool multiple
times.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-11-11 UTC."],[],[]]