I want to delete the whole collection using Flutter. So the entire student section I want to be gone.
I only found deleting single documents… and I don’t want to delete every document manually.
How do I automate that using Flutter.
Something like this: (just so you know how to attempt this)
final firebase = FirebaseFirestore.instance.collection('Students');
await firebase.delete;
Thanks for any help.
3
Answers
You can delete you collection by using
for loop
in.then
If there is an event that happens that should trigger the deletion of the collection, then I recommend you write a Cloud Function for that. This means that once the event happens, the function is triggered and the collection gets deleted internally.
@KushalHapani solution will work, however, iterating through all documents within the collection and deleting them in client-side code, will work safely only for small collections of documents. However, if your collection gets larger, it’s recommended to delete the documents in smaller batches to avoid out-of-memory errors.
You can delete a collection by retrieving the documents in the collection, loop through each document and call the delete method on its reference.