Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment