5

I have a Firebase Function that deletes a user's collection in a Firestore database when their account is deleted.

const firebase_tools = require("firebase-tools");
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();

exports.deleteUser = functions.auth.user().onDelete((user) => {
  return firebase_tools.firestore
      .delete(`users/${user.uid}`, {
        project: process.env.GCLOUD_PROJECT,
        token: functions.config().fb.token,
        recursive: true,
        yes: true
      }).catch((error) => {
        console.log(error);
        throw new functions.https.HttpsError(
            "unknown",
            "Error deleting user's data"
        );
      });
});

Whenever a user is deleted and the function is executed, I get the following error in the Functions logs.

FirebaseError: Missing required options (force) while running in non-interactive mode
    at prompt (/workspace/node_modules/firebase-tools/lib/prompt.js:16:15)
    at promptOnce (/workspace/node_modules/firebase-tools/lib/prompt.js:29:11)
    at Command.actionFn (/workspace/node_modules/firebase-tools/lib/commands/firestore-delete.js:69:51)
    at Object.delete (/workspace/node_modules/firebase-tools/lib/command.js:190:25)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

The only information I could find related to this is regarding deploying/deleting functions to Firebase and there's not much documentation for firebase-tools that I could find.

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
stripies
  • 182
  • 9

2 Answers2

9

Add force: true to the JSON passed to firebase-tools. Worked for me with version 10.1.4

{
        project: process.env.GCLOUD_PROJECT,
        token: functions.config().fb.token,
        recursive: true,
        yes: true,
        force: true // add this
}
pp__b
  • 123
  • 7
1

I've reproduced the error that you have encountered.

error

This error occurs on the latest "firebase-tools": "^10.1.3".

Based on the Delete data with a Callable Cloud Function, the documentation have sample code that still uses "firebase-tools": "9.18.0". You could downgrade your firebase-tools by modifying the package.json. E.g. below:

  "dependencies": {
    "firebase": "^9.6.5",
    "firebase-admin": "^9.12.0",
    "firebase-functions": "^3.16.0",
    "firebase-tools": "9.18.0"
  }

After downgrading, I'm able to delete the specified document successfully. working

You could also use what's @Renaud Tarnec answered by using Admin SDK. E.g. below:

const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();

db = admin.firestore();

exports.deleteUser = functions.auth.user().onDelete((user) => {
    db.collection("users").doc(user.uid).delete()
      .then(function(user) {
        console.log("Successfully Deleted User:", user.uid)
        })
      .catch((error) => {
        console.log(error);
        throw new functions.https.HttpsError(
            "unknown",
            "Error deleting user's data"
        );
      });
});
Marc Anthony B
  • 3,635
  • 2
  • 4
  • 19