Skip to content

Instantly share code, notes, and snippets.

Embed
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(
collections.map(async collection => {
// get documents
const documents = await collection.listDocuments();
await Promise.all(
documents.map(async 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.