Delete documents and fields
The following examples demonstrate how to delete documents, fields, and collections.
Delete documents
To delete a document, use the following language-specific delete()
methods:
Web version 9
Use the deleteDoc()
method:
import { doc, deleteDoc } from "firebase/firestore"; await deleteDoc(doc(db, "cities", "DC"));
Web version 8
Use the delete()
method:
db.collection("cities").doc("DC").delete().then(() => { console.log("Document successfully deleted!"); }).catch((error) => { console.error("Error removing document: ", error); });
Swift
Use the delete()
method:
do { try await db.collection("cities").document("DC").delete() print("Document successfully removed!") } catch { print("Error removing document: \(error)") }
Objective-C
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!"); } }];
Kotlin
Android
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) }
Java
Android
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); } });
Dart
Use the delete()
method:
db.collection("cities").doc("DC").delete().then( (doc) => print("Document deleted"), onError: (e) => print("Error updating document $e"), );
Java
Use the delete()
method:
Python
Use the delete()
method:
Python
(Async)
Use the delete()
method:
C++
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; } });
Node.js
Use the delete()
method:
Go
Use the Delete()
method:
PHP
Use the delete()
method:
Unity
Use the DeleteAsync()
method:
DocumentReference cityRef = db.Collection("cities").Document("DC"); cityRef.DeleteAsync();
C#
Use the DeleteAsync()
method:
Ruby
Use the delete()
method:
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/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:
Web version 9
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() });
Web version 8
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() });
Swift
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)") }
Objective-C
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"); } }];
Kotlin
Android
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 { }
Java
Android
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>() { // ... // ...
Dart
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);
Java
Use the FieldValue.delete()
method:
Python
Use the firestore.DELETE_FIELD
method:
Python
(Async)
Use the firestore.DELETE_FIELD
method:
C++
Use the FieldValue::Delete()
method:
DocumentReference doc_ref = db->Collection("cities").Document("BJ"); doc_ref.Update({{"capital", FieldValue::Delete()}}) .OnCompletion([](const Future<void>& future) { /*...*/ });
Node.js
Use the FieldValue.delete()
method:
Go
Use the firestore.Delete
method:
PHP
Use the FieldValue::deleteField()
method:
Unity
Use the FieldValue.Delete
method:
DocumentReference cityRef = db.Collection("cities").Document("BJ"); Dictionary<string, object> updates = new Dictionary<string, object> { { "Capital", FieldValue.Delete } };
C#
Use the FieldValue.Delete
method:
Ruby
Use the firestore.field_delete
method:
Delete collections
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 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.
Web
// Deleting collections from a Web client is not recommended.
Swift
// Deleting collections from an Apple client is not recommended.
Objective-C
// 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
Python
Python
(Async)
C++
// This is not supported. Delete data using CLI as discussed below.
Node.js
Go
PHP
Unity
// This is not supported. Delete data using CLI as discussed below.
C#
Ruby
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 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
Firestore supports bulk deleting one or more collection groups. For more information, see Bulk delete data.