How to delete all documents from a collection in Google Cloud Firestore?

To delete all documents from a Firebase collection in Firestore we need to get the collection, iterate over its elements and delete each one of them:

const db = new Firestore({
  projectId: "projectId",
  keyFilename: "./key.json"
});

db.collection("collectionName")
  .get()
  .then(res => {
    res.forEach(element => {
      element.ref.delete();
    });
  });

Comments

  1. when i try to use this, my collection got deleted too, how to only delete all the documents inside the collection without deleting the collection?

Leave a Reply

Your email address will not be published. Required fields are marked *