Skip to content

Instantly share code, notes, and snippets.

@potato4d
Created April 5, 2020 17:23
Show Gist options
  • Save potato4d/6848829bbd1785abe7043922e290d769 to your computer and use it in GitHub Desktop.
Save potato4d/6848829bbd1785abe7043922e290d769 to your computer and use it in GitHub Desktop.
import admin from 'firebase-admin'
const credential: any = null // 書き換えてね
const app = admin.initializeApp({
credential: admin.credential.cert(credential),
})
const splitByCount = (n: number, arr: admin.auth.UserRecord[]) => {
return arr.reduce((before: admin.auth.UserRecord[][], now: admin.auth.UserRecord) => {
if (Array.isArray(before[before.length - 1]) && before[before.length - 1].length === n) {
return [
...before,
[
now
]
]
} else {
return [
...(before.filter((l, i) => (i !== before.length - 1))),
[
...(before[before.length - 1] || []),
now
]
]
}
}, [] as admin.auth.UserRecord[][])
}
const delay = (ms: number) => {
return new Promise((resolve) => {
setTimeout(() => {
resolve()
}, ms)
})
}
// Delete
async function runDeleteAllUsers() {
const auth = app.auth() // cache
const {users} = await auth.listUsers(1000)
console.log('max users: ' + users.length)
const splitUsers = splitByCount(60, users)
let i = 0;
for (const users of splitUsers) {
i++;
console.log(`Set: ${i}`)
await Promise.all(users.map(async (user) => {
await auth.deleteUser(user.uid)
}))
console.log('---------------')
console.log(' wait ')
await delay(3000)
console.log('---------------')
}
console.log('Done')
process.exit(0)
}
runDeleteAllUsers()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment