57

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()})

Is it possible, and if so, how?

Juliano JC
  • 765
  • 1
  • 6
  • 13
  • [Here's the relevant docs for this](https://firebase.google.com/docs/firestore/manage-data/delete-data#fields) – Ben Jun 14 '23 at 21:53

7 Answers7

98

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()
});
ANDREYDEN
  • 96
  • 10
Sampath
  • 63,341
  • 64
  • 307
  • 441
  • 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. – Juliano JC Oct 28 '17 at 13:20
  • 2
    See the doc.It should work no? What about your `doc reference`? https://firebase.google.com/docs/firestore/manage-data/delete-data – Sampath Oct 28 '17 at 13:38
  • 1
    It was missing `var docRef = firebase.firestore ();` inside the constructor, I changed your code to `var docRef = this.db.doc(`ProfileUser/${userId}/followersCount/FollowersCount`); let removeCurrentUserId = docRef.update({ [currentUserId]: firebase.firestore.FieldValue.delete() }); ` now worked perfectly, thank you !!! – Juliano JC Oct 28 '17 at 14:03
  • Glad to hear that it helped :) – Sampath Oct 28 '17 at 14:04
  • `firebase.firestore.FieldValue.delete()` will not work for a field which is already `null` – ahmadalibaloch Jun 21 '20 at 13:00
  • You can just use `docRef.update([FIELDYOUWANTTOREMOVE: FieldValue.delete())` – Septronic Jun 09 '21 at 22:00
  • can someone tell me what happens when the field you are trying to delete doesn't exists on the document ? – Saurabh Apr 18 '22 at 05:20
  • 1
    @Saurabh You need to handle run time exception there. – Sampath Apr 18 '22 at 13:55
26

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

document.ref.update({
  FieldToDelete: admin.firestore.FieldValue.delete()
})
Dave
  • 261
  • 3
  • 2
22

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"
sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
2

IN CASE THE ABOVE ALL DIDNT WORKED FOR YOU (Like me) use this function

const deleteField = async() => {
    firestore.collection("users").get().then(function(querySnapshot) {
      querySnapshot.forEach(function(doc) {
          doc.ref.update({
              date_of_birth: firestore.FieldValue.delete()
          });
      });
  });
}
Aditya Singh
  • 135
  • 6
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 02 '23 at 15:24
  • 1
    This worked for me in the *admin SDK* context, however slightly adjusted: `admin.firestore().collection("users").where("userId", "==", userId).get().then((querySnapshot) => querySnapshot.forEach((doc) => doc.ref.update({ verificationUrl: admin.firestore.FieldValue.delete() })));` – Leon Vogler Jan 03 '23 at 12:49
1

carefully using this admin.firestore.FieldValue.delete() Because query doesn't work if you try to delete not available field on the document.

So, I think it's better to set null

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

OR

await db.doc(`ProfileUser/${userId}/followersCount/FollowersCount`)
            .set({[currentUserId]: null}, { merge: true })
BIS Tech
  • 17,000
  • 12
  • 99
  • 148
1

A solution that works on Firestore v2.7.3 (and probably higher versions) with python

from google.cloud import firestore

obj_ref = db.collection(f"{your_collection}").document("your_doc")

success = obj_ref.update({
    "to_be_deleted_field": firestore.DELETE_FIELD
})
Khalid
  • 41
  • 1
0

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
});
mim
  • 1,301
  • 14
  • 24
  • 1
    The part you said that didn't work for you, worked for me! Thanks. – moreirapontocom Jul 04 '20 at 01:43
  • 11
    Setting the value to `null` will now save the value as `null` in Firestore. – Unconventional Wisdom Sep 05 '20 at 03:36
  • 2
    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. – mim Sep 06 '20 at 18:22