Delete Collections and Subcollections

Deleting data in Cloud Firestore, especially from a resource-limited mobile app, can be difficult to implement correctly for the following reasons:

  • There is no operation that atomically deletes a collection.
  • Deleting a document does not delete the documents in its subcollections.
  • If your documents have dynamic subcollections, it can be hard to know what data to delete for a given path.
  • Deleting a collection of more than 500 documents requires multiple batched write operations or hundreds of single deletes.
  • In many apps, it isn't appropriate to give end-users permission to delete entire collections.

Fortunately, you can write a Cloud Function to run safe and performant deletes of entire collections or collection trees.

Before continuing, read about the Cloud Firestore data model.

Solution: Delete data with a Callable Cloud Function

This guide describes how to use a callable Cloud Function to delete data. Once you deploy this function, you can call it directly from your mobile app or website to recursively delete documents and collections.

To deploy the function and try a demo, see the sample code.

Cloud Function

The Cloud Function below deletes a collection and all of its descendants.

Instead of implementing your own recursive delete logic for you Cloud Function, you can take advantage of the firestore:delete command in the Firebase Command Line Interface (CLI). You can import any function of the Firebase CLI into a own Node.js application using the firebase-tools package.

The Firebase CLI uses the Cloud Firestore REST API to find all documents under the specified path and delete them individually. This implementation requires no knowledge of your app's specific data hierarchy and will even find and delete "orphaned" documents that no longer have a parent.

Node.js

/**
 * Initiate a recursive delete of documents at a given path.
 * 
 * The calling user must be authenticated and have the custom "admin" attribute
 * set to true on the auth token.
 * 
 * This delete is NOT an atomic operation and it's possible
 * that it may fail after only deleting some documents.
 * 
 * @param {string} data.path the document or collection path to delete.
 */
exports.recursiveDelete = functions
  .runWith({
    timeoutSeconds: 540,
    memory: '2GB'
  })
  .https.onCall((data, context) => {
    // Only allow admin users to execute this function.
    if (!(context.auth && context.auth.token && context.auth.token.admin)) {
      throw new functions.https.HttpsError(
        'permission-denied',
        'Must be an administrative user to initiate delete.'
      );
    }

    const path = data.path;
    console.log(
      `User ${context.auth.uid} has requested to delete path ${path}`
    );

    // Run a recursive delete on the given document or collection path.
    // The 'token' must be set in the functions config, and can be generated
    // at the command line by running 'firebase login:ci'.
    return firebase_tools.firestore
      .delete(path, {
        project: process.env.GCLOUD_PROJECT,
        recursive: true,
        yes: true,
        token: functions.config().fb.token
      })
      .then(() => {
        return {
          path: path 
        };
      });
  });

The Cloud Function above is implemented as a callable function which means it can be called directly from your mobile app or website as you would for a local function.

Client Invocation

To call the function, get a reference to the function from the Firebase SDK and pass the required parameters:

Web

/**
 * Call the 'recursiveDelete' callable function with a path to initiate
 * a server-side delete.
 */
function deleteAtPath(path) {
    var deleteFn = firebase.functions().httpsCallable('recursiveDelete');
    deleteFn({ path: path })
        .then(function(result) {
            logMessage('Delete success: ' + JSON.stringify(result));
        })
        .catch(function(err) {
            logMessage('Delete failed, see console,');
            console.warn(err);
        });
}

By using the client SDK for callable cloud functions, the users's authentication state and the path parameter are seamlessly passed to the remote function. When the function completes, the client will receive a callback with the result or an exception. To learn about how to call a cloud function from Android, iOS, or another platform, read the documentation.

Limitations

The solution shown above demonstrates deleting collections from a callable function, but you should be aware of the following limitations:

  • Consistency - the code above deletes documents one at a time. If you query while there is an ongoing delete operation, your results may reflect a partially complete state where only some targeted documents are deleted. There is also no guarantee that the delete operations will succeed or fail uniformly, so be prepared to handle cases of partial deletion.
  • Timeouts - the function above is configured to run for a maximum of 540 seconds before timing out. The deletion code can delete 4000 documents per second in the best case. If you need to delete more than 2,000,000 documents, you should consider running the operation on your own server so that it does not time out.

Send feedback about...

Need help? Visit our support page.