Juliano JC
Juliano JC

Reputation: 805

How to delete a field in a Firestore Document?

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?

Upvotes: 61

Views: 49208

Answers (7)

Khalid
Khalid

Reputation: 49

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

Upvotes: 1

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"

Upvotes: 25

Aditya Singh
Aditya Singh

Reputation: 105

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

Upvotes: 2

BIS Tech
BIS Tech

Reputation: 19434

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

Upvotes: 1

Dave
Dave

Reputation: 261

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

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

Upvotes: 26

mim
mim

Reputation: 1417

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

Upvotes: 0

Sampath
Sampath

Reputation: 65870

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

Upvotes: 101

Related Questions