0

I am deleting a doc which has sub-collections using a batched operation.

const myDocRef = firestore.collection("...").doc("...");
const myOtherDocRef = firestore.collection("...").doc("...");

const batch = firestore.batch();

batch.delete(myDocRef);
batch.delete(myOtherDocRef);
// TODO - Delete sub-collections ("likes", "comments")

batch.commit();

The document "myOtherDoc" is the one that has nested sub-collections. How can I delete them too, recursively, using the batch operation?

I mean, is it not possible to add some configuration for the recursively deletion?

Raul
  • 2,673
  • 1
  • 15
  • 52

1 Answers1

2

The documentation has detailed explanation on deleting collections recursively. You can copy the code from it and run in a cloud function.

Instead of implementing your own recursive delete logic for your Cloud Function, you can take advantage of the firestore:delete command in the Firebase Command Line Interface (CLI). You can import any function of the Firebase CLI into your Node.js application using the firebase-tools package.

Reference: https://firebase.google.com/docs/firestore/solutions/delete-collections#cloud_function

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84