Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Clears everything recursively in your Firestore database. Useful in the cleanUp routine of any tests.
const clearCollection = db => async collections => {
await Promise.all( collection => {
// get documents
const documents = await collection.listDocuments();
await Promise.all( doc => {
const collections = await db.doc(`${doc.path}`).listCollections();
// If document has a sub collection delete that first
if (collections.length > 0) {
await clearCollection(db)(collections);
await doc.delete();
const clearDB = async db => {
// get root collections
const collections = await db.listCollections();
// delete root collections
return clearCollection(db)(collections);
module.exports = {clearDB, clearCollection};
// clearDB(admin.firestore()) will delete everything in your database
// clearCollection(admin.firestore())(collectionRef) will delete everything within a collection
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.