Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete a Document with all Subcollections and Nested Subcollections in Firestore

How can you delete a Document with all it's collections and nested subcollections? (inside the functions environment)

In the RTDB you can ref.child('../someNode).setValue(null) and that completes the desired behavior.

I can think of two ways you could achieve the desired delete behavior, both with tremendously ghastly drawbacks.

  1. Create a 'Super' function that will spider every document and delete them in a batch. This function would be complicated, brittle to changes, and might take a lengthy execution time.

  2. Add 'onDelete' triggers for each Document type, and make it delete any direct subcollections. You'll call delete on the root document, and the deletion calls will propagate down the 'tree'. This is sluggish, scales atrociously and is costly due to the colossal load of function executions.

Imagine you would have to delete a 'GROUP' and all it's children. It would be deeply chaotic with #1 and pricey with #2 (1 function call per doc)

groups > GROUP > projects > PROJECT > files > FILE > assets > ASSET                                                    > urls > URL                                     > members > MEMBER                > questions > QUESTION > answers > ANSWER > replies > REPLY                                       > comments > COMMENT                > resources > RESOURCE > submissions > SUBMISSION                                       > requests > REQUEST 

Is there a superior/favored/cleaner way to delete a document and all it's nested subcollections?

It ought to be possible considering you can do it from the console.

like image 452
Linxy Avatar asked Mar 14 '18 20:03

Linxy


People also ask

How do I delete all Subcollections on firestore?

To delete an entire collection or subcollection in Cloud Firestore, retrieve all the documents within the collection or subcollection and delete them. If you have larger collections, you may want to delete the documents in smaller batches to avoid out-of-memory errors.

How do I delete multiple documents in firebase?

To delete multiple documents, you can do a single batched write. The WriteBatch class has a delete() method for this purpose.

Which of the following method is used to delete a field from a particular document in firebase?

Deleting Fields For deleting a specific field from a document, use the FieldValue. delete() method when we update a document.


1 Answers

according to firebase documentation:
https://firebase.google.com/docs/firestore/solutions/delete-collections
Deleting collection with nested subcollections might be done easy and neat with node-JS on the server side.

const client = require('firebase-tools'); await client.firestore       .delete(collectionPath, {         project: process.env.GCLOUD_PROJECT,         recursive: true,         yes: true       });  
like image 77
Oleg Bondarenko Avatar answered Oct 22 '22 22:10

Oleg Bondarenko