#!/usr/bin/env node /* Robot Clash - Firebase cleanup. DRY-RUN by default: it only lists what it WOULD delete. * * npm install firebase-admin (once, locally) * node admin/cleanup.js --key serviceAccount.json # preview, deletes nothing * node admin/cleanup.js --key serviceAccount.json --apply # actually delete * * Options: --db --stale-min * The service-account key is a SECRET: never commit or upload it (add it to .gitignore). * * Deletes (only with --apply): anonymous guest accounts (no e-mail), stale lobby/ presence, * and orphan saves/ ranks/ users/ nodes whose uid no longer exists in Auth. */ 'use strict'; const admin = require('firebase-admin'); const fs = require('fs'); function arg(name, def) { const i = process.argv.indexOf('--' + name); if (i < 0) return def; const next = process.argv[i + 1]; return next && !next.startsWith('--') ? next : true; } const keyPath = arg('key', process.env.GOOGLE_APPLICATION_CREDENTIALS); const DB = arg('db', 'https://robot-clash-aa37b-default-rtdb.firebaseio.com'); const APPLY = arg('apply', false) === true; const STALE_MIN = +arg('stale-min', 10); if (!keyPath || keyPath === true) { console.error('Need --key (or set GOOGLE_APPLICATION_CREDENTIALS).'); process.exit(1); } admin.initializeApp({ credential: admin.credential.cert(JSON.parse(fs.readFileSync(keyPath, 'utf8'))), databaseURL: DB, }); const auth = admin.auth(); const db = admin.database(); (async () => { console.log(APPLY ? '=== APPLY: deleting ===' : '=== DRY-RUN: nothing is deleted ==='); // 1) all auth users; flag anonymous (no e-mail and no linked provider) const uids = new Set(); const anon = []; let pageToken; do { const res = await auth.listUsers(1000, pageToken); for (const u of res.users) { uids.add(u.uid); const registered = !!u.email || (u.providerData && u.providerData.length > 0); if (!registered) anon.push(u.uid); } pageToken = res.pageToken; } while (pageToken); console.log(`Auth users: ${uids.size} total, ${anon.length} anonymous/guest`); // 2) stale lobby presence (no ts or older than the cutoff) const cutoff = Date.now() - STALE_MIN * 60000; const lobby = (await db.ref('lobby').get()).val() || {}; const staleLobby = Object.entries(lobby).filter(([, v]) => !v || !v.ts || v.ts < cutoff).map(([k]) => k); console.log(`Lobby: ${Object.keys(lobby).length} entries, ${staleLobby.length} stale (> ${STALE_MIN} min)`); // 3) orphan RTDB nodes (uid no longer exists in Auth) const orphans = {}; for (const node of ['saves', 'ranks', 'users']) { const data = (await db.ref(node).get()).val() || {}; orphans[node] = Object.keys(data).filter(uid => !uids.has(uid)); console.log(`${node}: ${Object.keys(data).length} entries, ${orphans[node].length} orphan`); } if (!APPLY) { console.log('\nWould delete:'); console.log(' anonymous accounts :', anon.length); console.log(' stale lobby :', staleLobby.length); for (const node of Object.keys(orphans)) console.log(` orphan ${node.padEnd(12)}:`, orphans[node].length); console.log('\nRe-run with --apply to actually delete.'); process.exit(0); } let n = 0; for (let i = 0; i < anon.length; i += 1000) { const r = await auth.deleteUsers(anon.slice(i, i + 1000)); n += r.successCount; } console.log(`Deleted ${n} anonymous accounts.`); for (const uid of staleLobby) await db.ref('lobby/' + uid).remove(); console.log(`Removed ${staleLobby.length} stale lobby entries.`); for (const node of Object.keys(orphans)) { for (const uid of orphans[node]) await db.ref(node + '/' + uid).remove(); console.log(`Removed ${orphans[node].length} orphan ${node}.`); } console.log('Done.'); process.exit(0); })().catch(e => { console.error(e); process.exit(1); });