I\'m looking for a way to clear an entire collection. I saw that there is a batch update option, but that would require me to know all of the document IDs in the collection.
The cleanest way I have found to delete all documents. The only time I would use this function is when using the emulator and you can simply paste the function into the console:
// Paste this in:
function clearCollection(path) {
const ref = firestore.collection(path)
ref.onSnapshot((snapshot) => {
snapshot.docs.forEach((doc) => {
ref.doc(doc.id).delete()
})
})
}
// Use it like this:
clearCollection('layers')
If you find yourself needing this code repeatedly save it as a snippet in Chrome and then you can have easy access to it and won't have to keep pasting the code block into the console. You must run the snippet before it is accessible from the code block. Documentation