Search code examples
node.jsfirebase-authenticationgoogle-cloud-functionsfirebase-admin

How to delete all users in Firebase auth?


I am trying to delete all users I have stored in Firebase auth.

Not sure why there is no "button" in the Firebas console that I can click or select multiple users at the same time. Anyway...

I tried setting up a callable cloud function like this:

export const deleteUsers = functions.https.onCall(
    async (req: any, res:any): Promise<any> => {
      const allUsers = await admin.listUsers();
      const allUsersUID = allUsers.users.map((user: { uid: any; }) => user.uid);
      return admin.deleteUsers(allUsersUID).then(() => res.send("deleted"));
    }
  );

However, when I try to call it from the firebase:shell using deleteUsers(), I get the following 400 error:

firebase > deleteUsers()
Sent request to function.
firebase > >  {"severity":"WARNING","message":"Request body is missing data."}
>  {"severity":"ERROR","message":"Invalid request, unable to process."}

RESPONSE RECEIVED FROM FUNCTION: 400, {
  "error": {
    "message": "Bad Request",
    "status": "INVALID_ARGUMENT"
  }
}

If I instead call it using deleteUsers({}) I get the following 500 error:

firebase > deleteUsers({})
Sent request to function.
firebase > >  {"verifications":{"app":"MISSING","auth":"MISSING"},"logging.googleapis.com/labels":{"firebase-log-type":"callable-request-verification"},"severity":"INFO","message":"Callable request verification passed"}
>  {"severity":"ERROR","message":"Unhandled error TypeError: res.on is not a function\n    at C:\\Users\\jastr\\Documents\\Company\\chess-marketplace\\node_modules\\firebase-functions\\lib\\common\\providers\\https.js:365:17\n    at new Promise (<anonymous>)\n    at C:\\Users\\jastr\\Documents\\Company\\chess-marketplace\\node_modules\\firebase-functions\\lib\\common\\providers\\https.js:364:16\n    at newHandler (C:\\Users\\jastr\\AppData\\Roaming\\npm\\node_modules\\firebase-tools\\lib\\emulator\\functionsEmulatorRuntime.js:315:16)\n    at fixedLen (C:\\Users\\jastr\\Documents\\Company\\chess-marketplace\\node_modules\\firebase-functions\\lib\\providers\\https.js:120:41)\n    at C:\\Users\\jastr\\Documents\\Company\\chess-marketplace\\node_modules\\firebase-functions\\lib\\common\\providers\\https.js:400:32\n    at processTicksAndRejections (node:internal/process/task_queues:96:5)\n    at async runFunction (C:\\Users\\jastr\\AppData\\Roaming\\npm\\node_modules\\firebase-tools\\lib\\emulator\\functionsEmulatorRuntime.js:547:9)\n    at async runHTTPS (C:\\Users\\jastr\\AppData\\Roaming\\npm\\node_modules\\firebase-tools\\lib\\emulator\\functionsEmulatorRuntime.js:573:5)\n    at async handler (C:\\Users\\jastr\\AppData\\Roaming\\npm\\node_modules\\firebase-tools\\lib\\emulator\\functionsEmulatorRuntime.js:493:17)"}

RESPONSE RECEIVED FROM FUNCTION: 500, {
  "error": {
    "message": "INTERNAL",
    "status": "INTERNAL"
  }
}

What am I doing wrong?


Solution

  • Invoking a callable Cloud Function requires that you follow its protocol or use an SDK that implements that protocol.

    If you just want to execute your code in a Node script, you can declare it as:

    export const deleteUsers = function() { 
      ...
    }