Skip to content

Instantly share code, notes, and snippets.

@cliffhall
Last active September 24, 2018 22:15
Show Gist options
  • Save cliffhall/94998f18323618112e83c05d4e509d0c to your computer and use it in GitHub Desktop.
Save cliffhall/94998f18323618112e83c05d4e509d0c to your computer and use it in GitHub Desktop.
Nuke a Firebase Database and All User Accounts Referenced Therein
// NOTE: Accounts that are not represented in your /users node will not be deleted!
// BLOG: There are other approaches, see: http://cliffordhall.com/2017/04/nuke-firebase-db-and-all-users/
"use strict";
// Get credentials and initialize firebase app
console.log("With the power vested in the admin user, lay waste the database and all its users!");
let admin = require("firebase-admin");
let serviceAccount = require([__dirname, "service-account-key.json"].join('/'));
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://your-db-name-here.firebaseio.com"
});
// Fetch the /users node and begin deleting users.
// Finally, wipe the database.
let user, users = [];
let usersRef = admin.database().ref('/users');
usersRef.once('value').then( (snapshot) => {
// Get an Array of users from the DatabaseSnapshot
snapshot.forEach( (childSnapshot) => {
user = childSnapshot.val();
users.push(user);
});
console.log(users.length + " users retrieved");
// Delete users then wipe the database
if (users.length > 0) {
// Now map users to an Array of Promises
console.log("Delete users... ");
let promises = users.map(user => deleteUser(user));
// Wait for all Promises to complete before wiping db
Promise.all(promises)
.then(wipeDatabase)
.catch( e => console.log(e.message) );
} else {
// If no users to delete, then just wipe database
wipeDatabase();
}
// Delete a user
// Promise always resolves, even if user doesn't exist,
// since a user can be deleted separately from their /users node
function deleteUser(user) {
return new Promise((resolve, reject) => {
console.log("Delete user: " + user.name + "");
admin.auth()
.deleteUser(user.uid)
.then( () => {
console.log(user.name + " deleted.");
resolve(user);
})
.catch( e => {
console.log([e.message, user.name, "could not be deleted!"].join(' '));
resolve(user);
});
});
}
// Wipe the database by removing the root node
function wipeDatabase() {
console.log("Wiping database... ");
admin.database().ref().remove()
.then( () => {
console.log('DONE!');
process.exit();
})
.catch( e => {
console.log(e.message);
process.exit();
});
}
});
{
"type": "service_account",
"project_id": "your-db-name-here",
"private_key_id": "b4158530b6255bd098369aa94f2313a60e1aa534",
"private_key": "-----BEGIN PRIVATE KEY-----\nyour-private-key-here\n-----END PRIVATE KEY-----\n",
"client_email": "firebase-adminsdk-xxxx@your-db-name-here.iam.gserviceaccount.com",
"client_id": "your-client-id-here",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-xxxx%40cyour-db-name-here.iam.gserviceaccount.com"
}
@chetstone
Copy link

Hmm. It deleted a few accounts, then started spitting out: "QUOTA_EXCEEDED : Exceeded quota for deleting accounts." Then it wiped the database and I had to go delete all the user accounts one by one in the console.

@chetstone
Copy link

Firebase account deletions are limited to 10/sec. https://firebase.google.com/docs/auth/limits.

I created a fork that just deletes the first 10 users.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment