How To Delete A Specific Field In Firebase Firestore Document

If you want to delete a specific field from a firebase document, use the FieldValue.delete() method when you update a document. The implementation will vary depending on what platform or language you use.

For the web it's generally this way:

1
2
3
const fruitRef = db.collection('fruits').doc('fruit_document'); 
// Remove the 'apple' field from the document 
const removeFruit = fruitRef.update({ apple: firebase.firestore.FieldValue.delete() }); 

But if you are using a NodeJs environment such as firebase functions: First you need to import firebase-admin to your nodejs app.

1
import * as admin from 'firebase-admin';

Next you get to get the firestore.FieldValue. To do so, simply define a new variable: const fieldValue = admin.firestore.FieldValue; With that we can use the fieldValue variable to get access to our firebase firestore document fields and delete them in an update operation call.

1
2
3
4
// Create a document reference 
const fruitRef = db.collection('fruits').doc('fruit_document'); 
// Remove the 'fruit' field from the document 
const removeFruit = fruitRef.update({ apple: fieldValue.delete() }); 

And here is the complete code for this:

1
2
3
4
5
6
import * as admin from 'firebase-admin`; 
const fieldValue = admin.firestore.FieldValue; 
// Create a document reference
const fruitRef = db.collection('fruits').doc('fruit_document');
// Remove the 'fruit' field from the document 
const removeFruit = fruitRef.update({ apple: fieldValue.delete() }); 

For more, go here https://firebase.google.com/docs/firestore/manage-data/delete-data#fields

0 Comments

12345

    00