Delete Field in Firestore Document

28,954

Solution 1

You can try as shown below:

// get the reference to the doc
let docRef=this.db.doc(`ProfileUser/${userId}/followersCount/FollowersCount`);

// remove the {currentUserId} field from the document
let removeCurrentUserId = docRef.update({
    [currentUserId]: firebase.firestore.FieldValue.delete()
});

Solution 2

This worked for me. (also worked to delete field with null value)

document.ref.update({
  FieldToDelete: admin.firestore.FieldValue.delete()
})

Solution 3

With Firebase Version 9 (Feb, 2022 Update):

If there is the collection "users" having one document(dWE72sOcV1CRuA0ngRt5) with the fields "name", "age" and "sex" as shown below:

users > dWE72sOcV1CRuA0ngRt5 > name: "John", 
                               age: 21, 
                               sex: "Male"

You can delete the field "age" with this code below:

import { doc, updateDoc, deleteField } from "firebase/firestore";

const userRef = doc(db, "users/dWE72sOcV1CRuA0ngRt5");

// Remove "age" field from the document
await updateDoc(userRef, {
  "age": deleteField()
});
users > dWE72sOcV1CRuA0ngRt5 > name: "John",  
                               sex: "Male"

You can delete multiple fields "age" and "sex" with this code below:

import { doc, updateDoc, deleteField } from "firebase/firestore";

const userRef = doc(db, "users/dWE72sOcV1CRuA0ngRt5");

// Remove "age" and "sex" fields from the document
await updateDoc(userRef, {
  "age": deleteField(),
  "sex": deleteField()
});
users > dWE72sOcV1CRuA0ngRt5 > name: "John"

Solution 4

For some reason the selected answer (firebase.firestore.FieldValue.delete()) did not work for me. but this did:

Simply set that field to null and it will be deleted!

// get the reference to the doc
let docRef=this.db.doc(`ProfileUser/${userId}/followersCount/FollowersCount`);

// remove the {currentUserId} field from the document
let removeCurrentUserId = docRef.update({
    [currentUserId]: null
});
Share:
28,954

Related videos on Youtube

Juliano JC
Author by

Juliano JC

Updated on July 09, 2022

Comments

  • Juliano JC
    Juliano JC almost 2 years

    how to delete a Document Field in Cloud Firestore? ... I'm using the code below but I can not.

    this.db.doc(`ProfileUser/${userId}/followersCount/FollowersCount`).update({ 
    [currentUserId]: firebase.firestore.FieldValue.delete()})
    

    Anyone know how to do it?

  • Juliano JC
    Juliano JC over 6 years
    Thanks for the reply. I tried this change in the code, but still does not delete the field, I made a test and I can update the value of boolean using similar code, but the delete function does not make any changes in the field.
  • Sampath
    Sampath over 6 years
    See the doc.It should work no? What about your doc reference? firebase.google.com/docs/firestore/manage-data/delete-data
  • Juliano JC
    Juliano JC over 6 years
    It was missing var docRef = firebase.firestore (); inside the constructor, I changed your code to var docRef = this.db.doc(ProfileUser/${userId}/followersCount/FollowersCo‌​unt); let removeCurrentUserId = docRef.update({ [currentUserId]: firebase.firestore.FieldValue.delete() }); now worked perfectly, thank you !!!
  • Sampath
    Sampath over 6 years
    Glad to hear that it helped :)
  • ahmadalibaloch
    ahmadalibaloch about 4 years
    firebase.firestore.FieldValue.delete() will not work for a field which is already null
  • moreirapontocom
    moreirapontocom almost 4 years
    The part you said that didn't work for you, worked for me! Thanks.
  • Unconventional Wisdom
    Unconventional Wisdom almost 4 years
    Setting the value to null will now save the value as null in Firestore.
  • mim
    mim almost 4 years
    Hmm, @NovelVentures I just tested it and it does remove the value if set to null! I have a feeling you are setting it to "null" as a string. ------------------------------------------------------------‌​------ PS: technically speaking, they would never store null or undefined in the database, ( of course these as string values are not null or undefined anymore! ), even an empty string (or no value) can't be stored.
  • Septronic
    Septronic about 3 years
    You can just use docRef.update([FIELDYOUWANTTOREMOVE: FieldValue.delete())
  • peter
    peter over 2 years
    This is the best answer ^^
  • dod_basim
    dod_basim over 2 years
    this worked for me for Google Functions Code / Admin SDK code
  • Saurabh
    Saurabh about 2 years
    can someone tell me what happens when the field you are trying to delete doesn't exists on the document ?
  • Sampath
    Sampath about 2 years
    @Saurabh You need to handle run time exception there.