Home > Net >  Firebase Functions - FirebaseError: Missing required options (force) while running in non-interactiv
Firebase Functions - FirebaseError: Missing required options (force) while running in non-interactiv

Time:02-01

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.

CodePudding user response:

With delete(`users/${user.uid}`) you are actually deleting only one document under the users collection, more precisely the document corresponding to the path users/${user.uid}.

You actually don't need to use the Firebase Command Line Interface (i.e. the firebase-tools library) to do that in a Cloud Function: you can very well interact with Firestore via the error

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

Based on the 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"
        );
      });
});
  •  Tags:  
  • Related