0
votes

I have a root collection students which has student documents and each of this document has some sub-collections. for eg: marks and each mark document has again sub-collections and so on till depth 4.

Now If i remove a particular marks sub-collection, I want all of its nested sub collections to be deleted completely instead of hanging orphaned.

I read the following open github issue https://github.com/firebase/firebase-admin-node/issues/361

and find out that deepDeleteCollection can be used only if we know that collection is a leaf sub-collection(reached max depth). but to get this information, we need to separately query by each document inside that sub collection, which doesn't makes sense in terms of performance.

What would be the best way to achieve deepCollectionDelete? Please let me know In case I seem to miss something here.

Thanks in Advance.

1

1 Answers

0
votes

but to get this information, we need to separately query by each document inside that sub collection, which doesn't makes sense in terms of performance.

Unlike in Firebase realtime database where to remove the whole structure within a particular node, you would have taken a reference and call removeValue() method, in Cloud Firestore this is not possible. In order to delete a document that contains a subcollection which in terms contains other documents with other subcollections, you need to find and remove all documents within subcollections from deeper hierarchy to higher hierarchy. For instance, let's assume you have a schema that looks like this:

Firestore-root
   |
   --- collectionOne
         |
         --- documentOne
         |      |
         |      --- subcollectionOne
         |              |
         |              --- documentTwo
         |                    |
         |                    --- subSubCollectionOne
         |                           |
         |                           --- //Documents
         |
         --- documentThree
                |
                --- subcollectionThree
                        |
                        --- // documents

To delete let's say documentOne, you need to get all documents within subSubCollectionOne and delete them and then find all documents within subcollectionOne and delete them and only at the end you should delete documentOne.

which doesn't makes sense in terms of performance.

This is not true. This process of deleting documents that exist in collections and subcollections works very well. You can delete the documents on client in smaller chunks or using a Cloud Function.