0

I can delete all documents in firestore one by one. But is there any efficient way to delete entire collection/batch, because one by one takes ages. Working but slow code:

public async Task DeleteCollection(string collectionReference)
        {  
            CollectionReference collection = db.Collection(collectionReference);
            QuerySnapshot snapshot = await collection.GetSnapshotAsync();
            IReadOnlyList<DocumentSnapshot> documents = snapshot.Documents;

            while (documents.Count > 0)
            {
                foreach (DocumentSnapshot document in documents)
                {
                    Console.WriteLine("Deleting document {0}", document.Id);
                    await document.Reference.DeleteAsync();
                }
                snapshot = await collection.GetSnapshotAsync();
                documents = snapshot.Documents;
            }
            Console.WriteLine("Finished deleting all documents from the collection.");
        }
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • The Firestore client libraries don't offer any way to delete an entire collection. What you are doing now is the only option if you are sticking with pure C# code. The duplicate question has other options if you are not using the client library. – Doug Stevenson Oct 26 '22 at 17:29

0 Answers0