Deleting documents in Firebase - Time & Space Complexity
When deleting documents in Firebase, it's important to know how the time it takes grows as you delete more items.
We want to understand how the number of delete operations changes when the number of documents increases.
Analyze the time complexity of the following operation sequence.
const deleteDocuments = async (docs) => {
for (const doc of docs) {
await firestore.collection('items').doc(doc.id).delete();
}
};
This code deletes each document one by one from the 'items' collection in Firebase.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Calling the delete() method on each document reference.
- How many times: Once for every document in the input list.
Each document requires one delete call, so as the number of documents grows, the number of delete calls grows the same way.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 delete calls |
| 100 | 100 delete calls |
| 1000 | 1000 delete calls |
Pattern observation: The number of delete operations grows directly with the number of documents.
Time Complexity: O(n)
This means the time to delete documents grows linearly with how many documents you want to delete.
[X] Wrong: "Deleting multiple documents at once takes the same time as deleting one document."
[OK] Correct: Each document requires its own delete call, so more documents mean more calls and more time.
Understanding how delete operations scale helps you design efficient data cleanup in real projects and shows you can think about resource use clearly.
"What if we used a batch delete operation instead of deleting documents one by one? How would the time complexity change?"