9

I needed a way to quickly clear an entire firestore DB but couldn't find great documentation for how to do it. Eventually I found on stack overflow this answer (Clear Firestore database of all data?) but was a bit nervous about how long it would take to clear my entire DB of millions of documents, so I want a way to just recursively delete a collection at a time.

Background: I've been running some tests migrating large amounts of data from an old DB to firestore, and after each run I want a clean slate to work with in firestore. Do NOT use this on production data!

Alex Egli
  • 1,884
  • 2
  • 24
  • 43

3 Answers3

37

This is now documented via recursiveDelete function:

Note that this is a relatively new feature, so you need to make sure your Firebase libraries are updated.

// Setup
const admin = require('firebase-admin');
const serviceAccount = require("./files/my-file.json");
admin.initializeApp({
  credential: admin.credential.cert(serviceAccount)
});
const firestore = admin.firestore();

// Delete
const documentRef = firestore
      .collection("users")
      .doc("M3S2iPhsiu2ZQmOK8ZcC");
await firestore.recursiveDelete(documentRef);
kgaidis
  • 14,259
  • 4
  • 79
  • 93
  • 4
    WOW this is beautiful. I wish this question and answer could get pushed to the top of S.O. for deleting subcollections. I feel like 98% answers here still explain the overly cumbersome process of batch-deleting individual documents in subcollections. Worked fast and beautifully for me. – Tristan Bennett Nov 29 '21 at 22:33
  • Even Firebase documentation still says you have to recursively traverse your documents/collections yourself. – Spock Dec 21 '21 at 22:05
  • 1
    Great answer! I'm glad I found this – Nobadi Jun 18 '22 at 23:49
  • @kgaid does `recursiveDelete` have any limit? – Sohel Islam Imran Sep 23 '22 at 21:01
  • 1
    weird this is still not in the docs, but please make sure you upgrade your firebase admin sdk or else you won't have access to this – max Dec 07 '22 at 03:35
3

I was able to do this by running the firestore cli delete command, specifying each collection by path. Make sure not to start the path with a leading slash, or it will think you are referring to a directory on your computer. Example of how to run:

firebase firestore:delete "path/to/collection" --recursive

The firestore:delete command is sort of documented here as well: https://firebase.google.com/docs/firestore/manage-data/delete-data#delete_data_with_the_firebase_cli

Update Please note that the command may fail after deleting about 20000 documents. I think it might be a limit it hits. I just re-run with the same collection path a few times to fully delete collections that are larger than 20k docs.

Alex Egli
  • 1,884
  • 2
  • 24
  • 43
1

From the Fireship website : https://fireship.io/snippets/delete-firestore-collection/ there is 2 options to delete collections in firestore :

Option 1 : You can manually delete a collection or subcollection from the Firebase Console OR by using the CLI :

firebase firestore:delete path-to-delete

Option 2 :

It is possible to interact with Firebase Tools from a Cloud Function. This works especially well with Callable functions because you most certainly want to enforce some form of user authorization. First, obtain CI token to authenticate firebase tools.

cd functions
npm i firebase-tools -D

firebase login:ci
# your_token

firebase functions:config:set ci_token='your_token'

The function should validate the user has permission to run the operation. If allowed, it runs the CLI command recursively on the collection and its nested subcollections.

const project = process.env.GCLOUD_PROJECT;
const token = functions.config().ci_token;

exports.deleteCollection = functions.runWith({ timeoutSeconds: 540})
  .https.onCall((data, context) => {

  const path = data.path;
  const allowed = context.auth.uid === path.split('/')[0]; // TODO your own logic

if (!allowed) {
  throw new functions.https.HttpsError(
    'permission-denied',
    'Hey, that is not cool buddy!'
  );
}

return firebase_tools.firestore
  .delete(path, {
    project,
    token,
    recursive: true,
    yes: true,
  })
  .then(() => ({ result: 'all done!' }));
});
Aion
  • 620
  • 1
  • 6
  • 25
  • This is specifically to run this command as an admin. The Firebase Console is very slow for deleting large collections so I would not recommend that. Cloud functions have runtime and resource limits so I would also not recommend them for very large collections. – Alex Egli Feb 02 '21 at 19:22