0
0
Firebasecloud~5 mins

Deleting fields in Firebase - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you need to remove a piece of information from a record in your database. Deleting fields helps you clean up or update your data without removing the whole record.
When a user updates their profile and removes their phone number.
When you want to clear out outdated information from a product listing.
When you need to remove a temporary flag or status from a document.
When cleaning up data to save storage space by removing unused fields.
When correcting mistakes by deleting incorrect data entries.
Commands
This command deletes the 'phone' field from the document with ID 'user123' in the 'users' collection. It updates only that field without affecting others.
Terminal
firebase firestore:update users/user123 --data '{"phone": "FieldValue.delete()"}'
Expected OutputExpected
Updated document users/user123
--data - Specifies the fields to update or delete in JSON format
This command retrieves the document 'user123' from the 'users' collection to verify that the 'phone' field has been deleted.
Terminal
firebase firestore:get users/user123
Expected OutputExpected
{ "name": "Alice", "email": "alice@example.com" }
Key Concept

If you remember nothing else from this pattern, remember: deleting a field removes just that piece of data without deleting the whole document.

Common Mistakes
Trying to delete a field by setting its value to null.
Setting a field to null keeps the field but with a null value; it does not remove the field itself.
Use FieldValue.delete() to remove the field completely.
Deleting a field without specifying the correct document path.
The command will fail or update the wrong document if the path is incorrect.
Double-check the collection and document ID before running the delete command.
Summary
Use FieldValue.delete() to remove specific fields from a Firestore document.
Run an update command targeting the document and field you want to delete.
Verify the deletion by retrieving the document and checking the fields.