Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Nuke a Firebase Database and All User Accounts Referenced Therein
// This fork limits deletion to the first 10 users
// and deletes the data for each user separately (rather than wiping the whole database)
// Because of the firebase quota of 10 deletions/second.
// https://firebase.google.com/docs/auth/limits
// 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,
'firebase-sadhana-firebase-adminsdk-lfpmb-cb8af8daeb.json',
].join('/'));
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: 'https://sadhana.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
users.length = Math.min(users.length, 10);
console.log(`Deleting ${users.length} users... `);
let promises = users.map(user => deleteUser(user));
// Wait for all Promises to complete before wiping db
Promise.all(promises)
.then(() => process.exit())
.catch(e => console.log(e.message));
} else {
// If no users to delete, then just wipe database
process.exit();
}
});
// 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.email + '');
admin
.auth()
.deleteUser(user.uid)
.then(() => {
console.log(user.email + ' deleted.');
return wipeUser(user);
})
.then(() => {
console.log(`deleted user data ${user.uid}`);
resolve(user);
})
.catch(e => {
console.log([e.message, user.email, 'could not be deleted!'].join(' '));
resolve(user);
});
});
}
// Wipe the database by removing the root node
function wipeUser(user) {
return new Promise((resolve, reject) => {
return usersRef
.child(user.uid)
.remove()
.then(() => resolve(user));
});
}
{
"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
You can’t perform that action at this time.