Skip to content

Instantly share code, notes, and snippets.

@koptionalsoftware
Last active November 15, 2019 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koptionalsoftware/f554634707058e6315e6efd5d0226fa2 to your computer and use it in GitHub Desktop.
Save koptionalsoftware/f554634707058e6315e6efd5d0226fa2 to your computer and use it in GitHub Desktop.
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