Bird
Raised Fist0
Firebasecloud~20 mins

Deleting documents in Firebase - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Firestore Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ service_behavior
intermediate
2:00remaining
What happens when you delete a Firestore document that does not exist?

You try to delete a document in Firestore using its path, but the document does not exist. What is the result?

Firebase
const docRef = firestore.collection('users').doc('nonexistentDoc');
await docRef.delete();
AThe delete operation throws an error indicating the document was not found.
BThe delete operation retries automatically until the document exists.
CThe delete operation creates an empty document at that path.
DThe delete operation succeeds silently without error.
Attempts:
2 left
💡 Hint

Think about how Firestore handles deletions for documents that are not present.

❓ Configuration
intermediate
2:00remaining
Which Firestore security rule allows deleting documents only if the user is authenticated?

Choose the Firestore security rule snippet that permits document deletion only when the user is signed in.

Firebase
service cloud.firestore {
  match /databases/{database}/documents {
    match /items/{itemId} {
      allow delete: if <condition>;
    }
  }
}
Arequest.auth != null
Brequest.auth == null
Crequest.auth.uid == 'admin'
Dtrue
Attempts:
2 left
💡 Hint

Check how to verify if a user is signed in within Firestore rules.

❓ Architecture
advanced
2:00remaining
How to efficiently delete all documents in a large Firestore collection?

You have a Firestore collection with thousands of documents. What is the best approach to delete all documents efficiently?

AUse Firestore client SDK to delete documents with a single query.
BDelete the collection directly using a single delete() call on the collection reference.
CUse a batch delete with Firestore Admin SDK, deleting documents in chunks of 500.
DDelete documents one by one in a loop without batching.
Attempts:
2 left
💡 Hint

Consider Firestore limits on batch operations and how to handle large data sets.

❓ security
advanced
2:00remaining
What security risk arises if Firestore delete rules allow all users to delete any document?

What is the main security risk if Firestore rules allow any authenticated user to delete any document in a collection?

AUsers can delete documents they do not own, causing data loss.
BUsers can read documents but cannot delete them.
CUsers can only delete their own documents, no risk involved.
DUsers can create new documents but not delete existing ones.
Attempts:
2 left
💡 Hint

Think about the principle of least privilege and data ownership.

🧠 Conceptual
expert
2:00remaining
What is the effect of deleting a Firestore document that has subcollections?

You delete a Firestore document that contains subcollections. What happens to the subcollections and their documents?

AThe document and all its subcollections and documents are deleted automatically.
BOnly the document is deleted; subcollections remain intact and accessible.
CThe delete operation fails if subcollections exist under the document.
DThe document is deleted and subcollections are archived automatically.
Attempts:
2 left
💡 Hint

Consider Firestore's data model and how deletions propagate.

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

  1. Step 1: Understand the purpose of delete()

    The delete() method is used to remove a document from Firestore.
  2. Step 2: Compare with other operations

    Adding, updating, and reading documents use different methods like set(), update(), and get().
  3. Final Answer:

    Removes a specific document from the database -> Option D
  4. 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

  1. Step 1: Identify correct method to delete a document

    The method to delete a document is delete() called on a document reference.
  2. Step 2: Check syntax for document reference

    Use collection('users').doc('doc1') to get the document, then call delete().
  3. Final Answer:

    db.collection('users').doc('doc1').delete() -> Option C
  4. 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?
db.collection('users').doc('doc1').delete()
  .then(() => console.log('Deleted'))
  .catch(() => console.log('Error'))
medium
A. Error
B. Deleted
C. Nothing
D. SyntaxError

Solution

  1. Step 1: Understand promise handling in delete()

    The delete() method returns a promise that resolves on success and rejects on failure.
  2. Step 2: Analyze the then/catch handlers

    If deletion succeeds, then() runs and logs 'Deleted'. If it fails, catch() logs 'Error'.
  3. Final Answer:

    Deleted -> Option B
  4. Quick Check:

    Successful delete logs 'Deleted' [OK]
Hint: Successful delete triggers then(), logs 'Deleted' [OK]
Common Mistakes:
  • Assuming delete() is synchronous
  • Ignoring promise handling
  • Expecting output without then()
4. Identify the error in this code snippet that tries to delete a document:
const docRef = db.collection('users').doc('doc1');
docRef.delete;
console.log('Deleted');
medium
A. Missing parentheses after delete method call
B. Wrong collection name
C. docRef is not defined
D. delete is not a method

Solution

  1. Step 1: Check method invocation syntax

    The delete method must be called with parentheses: delete().
  2. Step 2: Identify impact of missing parentheses

    Without parentheses, the method is not executed, so the document is not deleted.
  3. Final Answer:

    Missing parentheses after delete method call -> Option A
  4. 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?
hard
A. Use Promise.all with delete promises:
Promise.all(['doc1','doc2','doc3'].map(id => db.collection('users').doc(id).delete()))
  .then(() => console.log('All deleted'))
B. Call delete() on the collection:
db.collection('users').delete();
console.log('All deleted');
C. Use a loop with await:
for (const id of ['doc1','doc2','doc3']) {
  await db.collection('users').doc(id).delete();
}
console.log('All deleted');
D. Delete documents without waiting:
['doc1','doc2','doc3'].forEach(id => db.collection('users').doc(id).delete());
console.log('All deleted');

Solution

  1. Step 1: Understand asynchronous deletion of multiple documents

    Deleting multiple documents returns promises; to know when all finish, use Promise.all().
  2. Step 2: Evaluate each option's correctness

    Use Promise.all with delete promises:
    Promise.all(['doc1','doc2','doc3'].map(id => db.collection('users').doc(id).delete()))
      .then(() => console.log('All deleted'))
    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.
  3. Final Answer:

    Use Promise.all with delete promises -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard