Delete a Firestore field

Delete a Firestore field

Code sample

Go

_, err := client.Collection("cities").Doc("BJ").Update(ctx, []firestore.Update{
	{
		Path:  "capital",
		Value: firestore.Delete,
	},
})
if err != nil {
	// Handle any errors in an appropriate way, such as returning them.
	log.Printf("An error has occurred: %s", err)
}

Java

DocumentReference docRef = db.collection("cities").document("BJ");
Map<String, Object> updates = new HashMap<>();
updates.put("capital", FieldValue.delete());
// Update and delete the "capital" field in the document
ApiFuture<WriteResult> writeResult = docRef.update(updates);
System.out.println("Update time : " + writeResult.get());

Node.js

// Get the `FieldValue` object
const FieldValue = admin.firestore.FieldValue;

// Create a document reference
const cityRef = db.collection('cities').doc('BJ');

// Remove the 'capital' field from the document
const res = await cityRef.update({
  capital: FieldValue.delete()
});

Python

city_ref = db.collection(u'cities').document(u'BJ')
city_ref.update({
    u'capital': firestore.DELETE_FIELD
})

Ruby

city_ref = firestore.doc "#{collection_path}/BJ"
city_ref.update({ capital: firestore.field_delete })

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser