Jamie Jamier
Jamie Jamier

Reputation: 539

Firebase: deleting all the authenticated users

I am new to firebase and I wanted to create 10000 user with a script and I got blocked.

Now I want to delete them all and do it again but deleting it is a big problem now.

How can I delete all of these users? and make that 10000 users?

screenshot of random users I generated: screenshot

Thanks in advance.

Upvotes: 5

Views: 4991

Answers (4)

lenz
lenz

Reputation: 2425

Note that you can run the command from your shell using firebase functions:shell


If you have gcloud sdk installed:

From your terminal

  • login into firebase: firebase login

  • create a folder and cd into it (or use your existing project)
    mkdir sample_func && cd_$

  • initialize directory for firebase function

    • firebase init functions
    • follow prompts
  • Write the code in functions/index.js.
    Something like this:

    const functions = require("firebase-functions");
    const admin = require("firebase-admin");
    
    admin.initializeApp();
    
    // Delete all users in firebase auth
    exports.deleteAllUsers = functions.https.onRequest(async (req, res) => {
         const listUsers = await admin.auth().listUsers();
         for (const user of listUsers.users) {
             console.log(`Deleting user: ${user.email || 'anonymous'}`);
             await admin.auth().deleteUser(user.uid);
    
             // Wait to avoid hitting the rate limit. Note that this might cause
             // you function to timeout, in which case you might have to run the
             // functions multiple times. 
             await new Promise((resolve) => setTimeout(resolve, 100));
         }
         res.send("All users deleted");
    });
    
  • Run it locally.
    From within the folder:

    • firebase functions:shell
    • deleteAllUsers()

Upvotes: 0

Leo Lee
Leo Lee

Reputation: 1

Since Node.js firebase-admin version 8.12.0, delete multiple users is supported.

Here is the sample code.

admin.auth().deleteUsers([uid1, uid2, uid3])
  .then(deleteUsersResult => {
    console.log('Successfully deleted ' + deleteUsersResult.successCount + ' users');
    console.log('Failed to delete ' +  deleteUsersResult.failureCount + ' users');
    deleteUsersResult.errors.forEach(err => {
      console.log(err.error.toJSON());
    });
  })
  .catch(error => {
    console.log('Error deleting users:', error);
  });

Notice: the maximum number of users allowed to be deleted is 1000 per batch like list all users

Upvotes: 0

JASON G PETERSON
JASON G PETERSON

Reputation: 2223

Easier to do than you might think at first glance of the documentation.

A few lines will do it in Python:

import firebase_admin
from firebase_admin import credentials
from firebase_admin import auth

cred = credentials.Certificate("/path/to/downloaded/json/key/*.json")
firebase_admin.initialize_app(cred)

for user in auth.list_users().iterate_all():
    print("Deleting user " + user.uid)
    auth.delete_user(user.uid)

Upvotes: 8

Frank van Puffelen
Frank van Puffelen

Reputation: 600141

The easiest way to bulk delete users is likely through the Admin SDK which has an API to list users, and then to delete a user.

Upvotes: 1

Related Questions