Created
March 6, 2018 16:28
-
-
Save katowulf/da4e3d461e19a4795d6c43ac59dc5d08 to your computer and use it in GitHub Desktop.
Delete users in Firebase Authentication using Admin SDK
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const admin = require('firebase-admin'); | |
const serviceAccount = require('path/to/serviceAccountKey.json'); | |
// User IDs to be deleted | |
const UIDs = []; | |
// initialize the app | |
admin.initializeApp({ | |
credential: admin.credential.cert(serviceAccount), | |
databaseURL: 'https://<DATABASE_NAME>.firebaseio.com' | |
}); | |
// establish an interval to throttle requests | |
const originalCount = UIDs.length; | |
const intervalRef = setInterval(processNextTen, 1001); | |
function processNextTen() { | |
const uids = UIDs.splice(-10, 10); | |
if( uids.length ) { | |
const completed = originalCount - UIDs.length; | |
console.log(`[${completed}/${originalCount}] Deleting ${uids}`); | |
uids.forEach(deleteUser); | |
} | |
else { | |
cleanup(); | |
} | |
} | |
function deleteUser(uid) { | |
admin.auth().deleteUser(uid) | |
.catch(function(error) { | |
console.log("Error deleting user", uid, error); | |
}); | |
} | |
function cleanup() { | |
clearInterval(intervalRef); | |
console.log('finished'); | |
process.exit(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment