0
0
Firebasecloud~5 mins

Deleting documents in Firebase - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you need to remove data you no longer want in your app. Deleting documents in Firebase Firestore helps you keep your database clean and up to date by removing unwanted records.
When a user deletes their profile and you want to remove their data.
When you want to clear outdated or incorrect information from your app.
When cleaning up test data after development or debugging.
When removing items from a shopping cart after purchase.
When deleting logs or temporary data that is no longer needed.
Commands
This command deletes a specific document named 'my-document' inside 'my-collection' in Firestore. The --recursive flag ensures all nested data is deleted, and --yes skips confirmation to automate the process.
Terminal
firebase firestore:delete --project example-project --recursive --yes collections/my-collection/my-document
Expected OutputExpected
Deleted document collections/my-collection/my-document
--recursive - Deletes all nested collections and documents under the specified document.
--yes - Skips the confirmation prompt to delete immediately.
--project - Specifies which Firebase project to target.
This command tries to retrieve the deleted document to verify it no longer exists.
Terminal
firebase firestore:documents get --project example-project collections/my-collection/my-document
Expected OutputExpected
Error: Document not found: projects/example-project/databases/(default)/documents/my-collection/my-document
--project - Specifies which Firebase project to target.
Key Concept

If you remember nothing else from this pattern, remember: deleting a document removes it and all its nested data permanently from Firestore.

Common Mistakes
Running the delete command without the --recursive flag on a document that has nested collections.
Only the document itself is deleted, but nested collections remain, causing leftover data.
Always use --recursive to delete the document and all nested collections completely.
Not specifying the correct project with --project flag.
The command may run on the wrong Firebase project, deleting unintended data or failing to find the document.
Always include --project with your Firebase project ID to target the right database.
Skipping verification after deletion.
You might think the document is deleted but it still exists due to errors or missing flags.
Run a get command after deletion to confirm the document no longer exists.
Summary
Use the firebase firestore:delete command with --recursive and --yes flags to delete documents and nested data.
Always specify the correct Firebase project with the --project flag.
Verify deletion by attempting to get the document after deletion.