To delete all documents in a Firestore collection in JavaScript, you can use the following code:
const db = firebase.firestore();
const collectionRef = db.collection('your_collection_name');
collectionRef.get()
.then(querySnapshot => {
querySnapshot.forEach(doc => {
doc.ref.delete();
});
})
.catch(error => {
console.error("Error deleting collection: ", error);
});
This code will retrieve all documents in the collection using the get()
method, iterate through each document and call the delete()
method on the document reference to delete it. Note that this cannot be undone, so use with caution.
Asked: 2022-05-12 11:00:00 +0000
Seen: 7 times
Last updated: Feb 06 '22