Delete all users from firebase auth console

后端 未结 16 1643
臣服心动
臣服心动 2020-12-04 11:53

Is there an easy way to delete all registered users from firebase console? For example, I created a hundred users from my development environment, and now I want to delete a

16条回答
  •  情深已故
    2020-12-04 12:37

    Fully working solution using Firebase Admin SDK

    Using the Firebase Admin SDK is really easy and the recommended way to perform such tasks on your Firebase data. This solution is unlike the other makeshift solutions using the developer console.

    I just put together a Node.js script to delete all users in your Firebase authentication. I have already tested it by deleting ~10000 users. I simply ran the following Node.js code.

    To setup Firebase Admin SDK

    Create a new folder. Run the following in terminal

    npm init
    sudo npm install firebase-admin --save
    

    Now create an index.js file in this folder.

    Steps to follow:

    • Go to your Firebase project -> Project Settings -> Service Accounts.
    • Click on Generate new Private Key to download the JSON file. Copy the path to JSON file and replace it in the code below in the path to service accounts private key json file.
    • Also, copy the databaseURL from the settings page. Replace it in the code.
    • Copy and paste the code in index.js.
    • Run in terminal node index.js. Watch the mayhem!
    var admin = require('firebase-admin');
    
    var serviceAccount = require("/path/to/service/accounts/private/key/json/file");
    
    admin.initializeApp({
        credential: admin.credential.cert(serviceAccount),
        databaseURL: "/url/to/your/database"
    });
    
    function deleteUser(uid) {
        admin.auth().deleteUser(uid)
            .then(function() {
                console.log('Successfully deleted user', uid);
            })
            .catch(function(error) {
                console.log('Error deleting user:', error);
            });
    }
    
    function getAllUsers(nextPageToken) {
        admin.auth().listUsers(100, nextPageToken)
            .then(function(listUsersResult) {
                listUsersResult.users.forEach(function(userRecord) {
                    uid = userRecord.toJSON().uid;
                    deleteUser(uid);
                });
                if (listUsersResult.pageToken) {
                    getAllUsers(listUsersResult.pageToken);
                }
            })
            .catch(function(error) {
                console.log('Error listing users:', error);
            });
    }
    
    getAllUsers();
    

提交回复
热议问题