The following examples demonstrate how to delete documents, fields, and collections.
Delete documents
To delete a document, use the following language-specific delete()
methods:
Use the deleteDoc()
method:
import { doc, deleteDoc } from "firebase/firestore"; await deleteDoc(doc(db, "cities", "DC"));
Use the delete()
method:
db.collection("cities").doc("DC").delete().then(() => { console.log("Document successfully deleted!"); }).catch((error) => { console.error("Error removing document: ", error); });
Use the delete()
method:
do { try await db.collection("cities").document("DC").delete() print("Document successfully removed!") } catch { print("Error removing document: \(error)") }
Use the deleteDocumentWithCompletion:
method:
[[[self.db collectionWithPath:@"cities"] documentWithPath:@"DC"] deleteDocumentWithCompletion:^(NSError * _Nullable error) { if (error != nil) { NSLog(@"Error removing document: %@", error); } else { NSLog(@"Document successfully removed!"); } }];
Use the delete()
method:
db.collection("cities").document("DC") .delete() .addOnSuccessListener { Log.d(TAG, "DocumentSnapshot successfully deleted!") } .addOnFailureListener { e -> Log.w(TAG, "Error deleting document", e) }
Use the delete()
method:
db.collection("cities").document("DC") .delete() .addOnSuccessListener(new OnSuccessListener<Void>() { @Override public void onSuccess(Void aVoid) { Log.d(TAG, "DocumentSnapshot successfully deleted!"); } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { Log.w(TAG, "Error deleting document", e); } });
Use the delete()
method:
db.collection("cities").doc("DC").delete().then( (doc) => print("Document deleted"), onError: (e) => print("Error updating document $e"), );
Use the delete()
method:
Use the delete()
method:
Use the delete()
method:
Use the Delete()
method:
db->Collection("cities").Document("DC").Delete().OnCompletion( [](const Future<void>& future) { if (future.error() == Error::kErrorOk) { std::cout << "DocumentSnapshot successfully deleted!" << std::endl; } else { std::cout << "Error deleting document: " << future.error_message() << std::endl; } });
Use the delete()
method:
Use the Delete()
method:
Use the delete()
method:
Use the DeleteAsync()
method:
DocumentReference cityRef = db.Collection("cities").Document("DC"); cityRef.DeleteAsync();
Use the DeleteAsync()
method:
Use the delete()
method:
When you delete a document, Cloud 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/mysubdoc
even
if you delete the ancestor document at /mycoll/mydoc
.
Non-existent ancestor documents appear 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, see Delete Collections.
Delete fields
To delete specific fields from a document, use the following language-specific FieldValue.delete()
methods
when you update a document:
Use the deleteField()
method:
import { doc, updateDoc, deleteField } from "firebase/firestore"; const cityRef = doc(db, 'cities', 'BJ'); // Remove the 'capital' field from the document await updateDoc(cityRef, { capital: deleteField() });
Use the FieldValue.delete()
method:
var cityRef = db.collection('cities').doc('BJ'); // Remove the 'capital' field from the document var removeCapital = cityRef.update({ capital: firebase.firestore.FieldValue.delete() });
Use the FieldValue.delete()
method:
do { try await db.collection("cities").document("BJ").updateData([ "capital": FieldValue.delete(), ]) print("Document successfully updated") } catch { print("Error updating document: \(error)") }
Use the fieldValueForDelete:
method:
[[[self.db collectionWithPath:@"cities"] documentWithPath:@"BJ"] updateData:@{ @"capital": [FIRFieldValue fieldValueForDelete] } completion:^(NSError * _Nullable error) { if (error != nil) { NSLog(@"Error updating document: %@", error); } else { NSLog(@"Document successfully updated"); } }];
Use the FieldValue.delete()
method:
val docRef = db.collection("cities").document("BJ") // Remove the 'capital' field from the document val updates = hashMapOf<String, Any>( "capital" to FieldValue.delete(), ) docRef.update(updates).addOnCompleteListener { }
Use the FieldValue.delete()
method:
DocumentReference docRef = db.collection("cities").document("BJ"); // Remove the 'capital' field from the document Map<String,Object> updates = new HashMap<>(); updates.put("capital", FieldValue.delete()); docRef.update(updates).addOnCompleteListener(new OnCompleteListener<Void>() { // ... // ...
Use the FieldValue.delete()
method:
final docRef = db.collection("cities").doc("BJ"); // Remove the 'capital' field from the document final updates = <String, dynamic>{ "capital": FieldValue.delete(), }; docRef.update(updates);
Use the FieldValue.delete()
method:
Use the firestore.DELETE_FIELD
method:
Use the firestore.DELETE_FIELD
method:
Use the FieldValue::Delete()
method:
DocumentReference doc_ref = db->Collection("cities").Document("BJ"); doc_ref.Update({{"capital", FieldValue::Delete()}}) .OnCompletion([](const Future<void>& future) { /*...*/ });
Use the FieldValue.delete()
method:
Use the firestore.Delete
method:
Use the FieldValue::deleteField()
method:
Use the FieldValue.Delete
method:
DocumentReference cityRef = db.Collection("cities").Document("BJ"); Dictionary<string, object> updates = new Dictionary<string, object> { { "Capital", FieldValue.Delete } };
Use the FieldValue.Delete
method:
Use the firestore.field_delete
method:
Delete collections
To delete an entire collection or subcollection in Cloud 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 snippets below are somewhat simplified and do not deal with error handling, security, deleting subcollections, or maximizing performance. To learn more about one recommended approach to deleting collections in production, see Deleting Collections and Subcollections.
// Deleting collections from a Web client is not recommended.
// Deleting collections from an Apple client is not recommended.
// Deleting collections from an Apple client is not recommended.
// Deleting collections from an Android client is not recommended.
// Deleting collections from an Android client is not recommended.
Deleting collections from the client is not recommended.
// This is not supported. Delete data using CLI as discussed below.
// This is not supported. Delete data using CLI as discussed below.
Delete data with the Firebase CLI
You can also use the Firebase CLI to delete documents and collections. Use the following command to delete data:
firebase firestore:delete [options] <<path>>
Delete data with the console
You can delete documents and collections from the Cloud Firestore page in the console. Deleting a document from the console deletes all of the nested data in that document, including any subcollections.
Delete data with TTL policies
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.
For information about setting TTL, see Manage data retention with TTL policies.
For more information on error codes and how to resolve latency issues when deleting data check out the troubleshooting page.
Delete data with Dataflow
Dataflow is a great tool for bulk operations on your Firestore database. The Firestore connector for Dataflow introduction blog post has an example of deleting all documents in a collection group.
Use managed bulk delete
Cloud Firestore supports bulk deleting one or more collection groups. For more information, see Bulk delete data.