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
});
Related videos on Youtube
Author by
Juliano JC
Updated on July 09, 2022Comments
-
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 over 6 yearsThanks 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 over 6 yearsSee the doc.It should work no? What about your
doc reference
? firebase.google.com/docs/firestore/manage-data/delete-data -
Juliano JC over 6 yearsIt was missing
var docRef = firebase.firestore ();
inside the constructor, I changed your code tovar docRef = this.db.doc(
ProfileUser/${userId}/followersCount/FollowersCount); let removeCurrentUserId = docRef.update({ [currentUserId]: firebase.firestore.FieldValue.delete() });
now worked perfectly, thank you !!! -
Sampath over 6 yearsGlad to hear that it helped :)
-
ahmadalibaloch about 4 years
firebase.firestore.FieldValue.delete()
will not work for a field which is alreadynull
-
moreirapontocom almost 4 yearsThe part you said that didn't work for you, worked for me! Thanks.
-
Unconventional Wisdom almost 4 yearsSetting the value to
null
will now save the value asnull
in Firestore. -
mim almost 4 yearsHmm, @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 storenull
orundefined
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 about 3 yearsYou can just use
docRef.update([FIELDYOUWANTTOREMOVE: FieldValue.delete())
-
peter over 2 yearsThis is the best answer ^^
-
dod_basim over 2 yearsthis worked for me for Google Functions Code / Admin SDK code
-
Saurabh about 2 yearscan someone tell me what happens when the field you are trying to delete doesn't exists on the document ?
-
Sampath about 2 years@Saurabh You need to handle run time exception there.