Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I delete a document with all its subcollections in firebase with flutter and dart

Every time I try to delete a document programmatically using documentSnapshot.delete(), the firebase only deletes the document and not its subcollections. I want to permanently delete a document and all its subcollections with the push of a button and every time the user tries to create the same document, it'd be empty.

What's the proper way to do that?

(CLOSED)

This is the code that worked for me:

CollectionReference<Map<String, dynamic>> collectionReference = FirebaseFirestore.instance.collection('collection name').doc('document name').collection('collection name');
DocumentReference<Map<String, dynamic>> documentReference = collectionReference.doc('document name');

documentReference.collection('lists').get().then((lists) {
    lists.docs.forEach((listElement) {
      documentReference.collection('lists').doc(listElement.id.toString()).collection('cards').get().then((cards) {
        cards.docs.forEach((cardElement) {
          documentReference.collection('lists').doc(listElement.id.toString()).collection('cards').doc(cardElement.id.toString()).delete();
        });
      });
      documentReference.collection('lists').doc(listElement.id.toString()).delete();
    });
  });

await documentReference.delete();
like image 941
Gkon Avatar asked Nov 20 '25 20:11

Gkon


2 Answers

Deleting a document does not automatically delete all documents in its sub-collections.

There is no method in firestore API to delete subcollections along with the document. You need to loop through and delete the documents in subcollections and then delete the parent document.

Here's a link for better understanding.

like image 77
bharats Avatar answered Nov 22 '25 08:11

bharats


I don't know Dart but here's my simple solution with PHP Firestore;

public function destroyCollection(CollectionReference $collection)
{
    $documents = [];
    foreach ($collection->listDocuments() as $document) {
        $documents[] = $document;
    }

    $this->destroyDocuments($documents);
}

/**
 * @param DocumentReference[] $documents
 */
public function destroyDocuments(array $documents)
{
    if (!$documents) return;
    
    $batch = $this->database->getBatch();
    foreach ($documents as $document) {
        $batch->delete($document);

        foreach ($document->collections() as $collection) {
            $this->destroyCollection($collection);
        }
    }
    
    $batch->commit();
}

It'll delete all the documents that it finds no matter how deeply nested they are. And non-existing docs won't show up in snapshots and queries so that's why I'm using the listDocuments() method even tho I'm sacrificing performance a bit.

like image 33
theChadOfBrogrammers Avatar answered Nov 22 '25 09:11

theChadOfBrogrammers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!