Usama Abdul Razzaq
Usama Abdul Razzaq

Reputation: 803

how to delete a complete collection including sub-collection in firebase firestore

I am making an application in which i have to make invoice of cars and the pattern i am using to store the invoice is shown in image below parent collection("containerInvoice")

sub-collection/child collection ("container")

sub child collection ("units")

  containerInvoice: {
                    invoiceNo:123,  
                    total:$9000,
                    container:{
                               containerNo:911,
                               containerTotal:$201999
                               units:{
                                      id:199,
                                      chasis:a829nn, 
                                     }
                              }
                    }

if i directly delete containerInvoice's document then it is delete successfully but it leave it's sub-collection and sub-sub-collection undelete.

the way i fount to delete whole collection including its child collection is to access these documents by document's id and delete it, but it is a lengthy process.

Is there any way to delete a parent collection and all the child collection deleted automatically

Upvotes: 2

Views: 3888

Answers (1)

Javier A
Javier A

Reputation: 569

As @danh pointed out, according to the documentation, when you delete a document, Cloud Firestore does not automatically delete the documents within its subcollections.

If you want to delete a document and all the documents within its subcollections, you must do so manually [1].

To delete an entire collection or subcollection in Cloud Firestore, retrieve all the documents within the collection or subcollection and delete them [2].

[1] - https://firebase.google.com/docs/firestore/manage-data/delete-data#delete_documents

[2] - https://firebase.google.com/docs/firestore/manage-data/delete-data#collections

Upvotes: 2

Related Questions