📖 Scenario: You are managing a simple app that stores user profiles in Firebase Firestore. Sometimes, you need to remove a user profile when it is no longer needed.
🎯 Goal: Learn how to delete a specific document from a Firestore collection using Firebase SDK.
📋 What You'll Learn
Create a Firestore collection reference
Specify the document ID to delete
Use the Firebase SDK method to delete the document
Confirm the deletion code is correctly structured
💡 Why This Matters
🌍 Real World
Deleting documents is common when users remove their accounts or when data needs cleanup in apps using Firestore.
💼 Career
Understanding how to delete documents safely and correctly is essential for backend and full-stack developers working with Firebase.
Progress0 / 4 steps
1
Set up Firestore collection reference
Create a constant called usersCollection that references the Firestore collection named users using collection(db, 'users').
Firebase
Hint
Use the collection function from Firebase Firestore and pass db and the collection name 'users'.
2
Specify the document ID to delete
Create a constant called userIdToDelete and set it to the string 'user123', which is the ID of the user document to delete.
Firebase
Hint
Assign the exact string 'user123' to the constant userIdToDelete.
3
Create a document reference for deletion
Create a constant called userDocRef that references the document to delete by using doc(usersCollection, userIdToDelete).
Firebase
Hint
Use the doc function with usersCollection and userIdToDelete to get the document reference.
4
Delete the document using Firebase SDK
Write an async function called deleteUserDocument that calls deleteDoc(userDocRef) to delete the document. Use await inside the function.
Firebase
Hint
Define an async function and use await deleteDoc(userDocRef) inside it to delete the document.
Practice
(1/5)
1. What does the delete() method do in Firebase Firestore?
easy
A. Reads data from a document
B. Adds a new document to the database
C. Updates fields in an existing document
D. Removes a specific document from the database
Solution
Step 1: Understand the purpose of delete()
The delete() method is used to remove a document from Firestore.
Step 2: Compare with other operations
Adding, updating, and reading documents use different methods like set(), update(), and get().
Final Answer:
Removes a specific document from the database -> Option D
Quick Check:
delete() removes documents [OK]
Hint: Delete removes documents, not adds or updates [OK]
Common Mistakes:
Confusing delete() with set() or update()
Thinking delete() reads data
Assuming delete() removes collections
2. Which of the following is the correct syntax to delete a document with ID 'doc1' from a collection 'users' in Firestore?
easy
A. db.collection('users').doc('doc1').remove()
B. db.collection('users').delete('doc1')
C. db.collection('users').doc('doc1').delete()
D. db.delete('users/doc1')
Solution
Step 1: Identify correct method to delete a document
The method to delete a document is delete() called on a document reference.
Step 2: Check syntax for document reference
Use collection('users').doc('doc1') to get the document, then call delete().
Final Answer:
db.collection('users').doc('doc1').delete() -> Option C
Quick Check:
Use doc().delete() to remove documents [OK]
Hint: Use doc('id').delete() to remove a document [OK]
Common Mistakes:
Using remove() instead of delete()
Calling delete() on collection instead of doc
Wrong method chaining order
3. What will the following code output if the document 'doc1' exists and is deleted successfully?
The delete method must be called with parentheses: delete().
Step 2: Identify impact of missing parentheses
Without parentheses, the method is not executed, so the document is not deleted.
Final Answer:
Missing parentheses after delete method call -> Option A
Quick Check:
Call delete() with () to execute [OK]
Hint: Always use parentheses to call delete() method [OK]
Common Mistakes:
Forgetting () after delete
Assuming delete is a property, not a method
Ignoring promise handling
5. You want to delete multiple documents with IDs ['doc1', 'doc2', 'doc3'] from the 'users' collection. Which approach correctly deletes all documents and logs 'All deleted' after completion?
uses Promise.all() with delete promises to wait for all deletions concurrently before logging. B calls invalid delete() on collection. C uses await loop without async function, causing syntax error. D logs without waiting for completion.
Final Answer:
Use Promise.all with delete promises -> Option A
Quick Check:
Promise.all waits for all deletes before logging [OK]
Hint: Use Promise.all to wait for all deletes before logging [OK]
Common Mistakes:
Trying to delete entire collection directly
Not waiting for all deletes before logging
Using forEach without async/await or Promise.all
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.