19

I'm making a Flutter application.

But, I cannot delete a field in the Firestore document. In another language I know to use FieldValue.delete() to delete a file in Firestorm document.

In Dart, How do I delete?

Samet ÖZTOPRAK
  • 3,112
  • 3
  • 32
  • 33
sekitaka
  • 1,010
  • 2
  • 14
  • 30

5 Answers5

39

Update Oct,2018: This is Now Possible:

In order to delete a particular field from a Cloud Firestore document - make sure you are using Plugin version 0.8.0 or Above. Now a E.g If you have a document having a field 'Desc' with contain some Text. In Order to Delete it.

Firestore.instance.collection('path').document('name').update({'Desc': FieldValue.delete()}).whenComplete((){
  print('Field Deleted');
});

This will Delete 'Desc' Field from the Document 'name'

Mutlu Simsek
  • 1,088
  • 14
  • 22
anmol.majhail
  • 48,256
  • 14
  • 136
  • 105
  • This solution doesn't seem to work anymore, I asked a separate question using your code [here](https://stackoverflow.com/questions/60759410/error-in-deleting-specific-field-in-a-cloud-firestore-document) – iDecode Mar 19 '20 at 15:16
3

I think this is currently impossible in standard, non hacky way. There is an open issue https://github.com/flutter/flutter/issues/13905 in Flutter which have to be resolved first.

antygravity
  • 1,307
  • 10
  • 12
3

Get the DocumentReference, invoke update with a map which has a key you want to delete and value FieldValue.delete().

var collection = FirebaseFirestore.instance.collection('collection');
collection
  .doc('document_id')
  .update({
    'field_to_delete': FieldValue.delete(),
    }
);
CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
3

Here is an official solution for deleting Field Path when programming in Flutter/Dart

Here is how a sample Firestore collection looks like:

{
   'path': {
      'name': { 
        'title': 'my title'
        'description': 'my description',
       }
   }
}

In order to delete description field, you can do this:

Firestore.instance.collection('path').document('name').set(
    {'description': FieldValue.delete()},
    SetOptions(
      merge: true,
    ),
)

And the collection will look like this:

{
   'path': {
      'name': { 
        'title': 'my title'
       }
   }
}
lcsvcn
  • 1,184
  • 3
  • 13
  • 28
1

if you have nested fields then use the '.' (Dot) notation to specify the field.

E.g if your data is nested Map then this is handy.

Firestore.instance.collection('path').document('name').update({'address.town': FieldValue.delete()}).whenComplete((){
  print('Field Deleted');
});
Mathulan
  • 530
  • 5
  • 7
  • Thanks for the answer, I have other question, if I have field with multiple items, like "comments": [cmt1, cmt2, ...], than how to delete a specific item ? – AmegoDev. May 07 '22 at 07:24