Created
January 13, 2021 10:26
-
-
Save samtstern/a7f0cff8bf0861f1040cdd238caf1762 to your computer and use it in GitHub Desktop.
Firestore Bulk Writer Delete
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'); | |
// TODO(you): Change this to match your Project ID | |
admin.initializeApp({ | |
projectId: "YOUR-PROJECT-ID" | |
}); | |
const READ_BATCH_SIZE = 250; | |
const db = admin.firestore(); | |
async function main() { | |
// TODO(you): Change this to be a query that matches your needs | |
const baseQuery = db.collection('foo').where('bar', '==', 'baz'); | |
// Keep track of query pagination | |
let startAfterDoc = undefined; | |
let hasMore = true; | |
// This new Bulk Writer API will help optimize your | |
// delete batches for you. | |
const writer = db.bulkWriter(); | |
// Keep track of stats | |
let docsRead = 0; | |
let docsDeleted = 0; | |
writer.onWriteResult((ref) => { | |
docsDeleted++; | |
// Log a message every 1000 docs | |
if (docsDeleted % 1000 === 0) { | |
console.log(`[${new Date().toTimeString()}]: Read ${docsRead}, Deleted ${docsDeleted}`); | |
} | |
}); | |
while (hasMore) { | |
let q = baseQuery.orderBy('__name__'); | |
if (startAfterDoc) { | |
q = baseQuery.startAfter(startAfterDoc); | |
} | |
q = q.limit(READ_BATCH_SIZE); | |
// Read a batch of docs | |
const snap = await q.get(); | |
docsRead += snap.docs.length; | |
// Tell BulkWriter to delete them all, but don't wait on it | |
for (const d of snap.docs) { | |
writer.delete(d.ref); | |
} | |
// If there are any results, read another page | |
hasMore = snap.docs.length > 0; | |
if (hasMore) { | |
startAfterDoc = snap.docs[snap.docs.length - 1]; | |
} | |
} | |
// Wait for all of the deletes to finish now | |
await writer.flush(); | |
await writer.close(); | |
} | |
main(); |
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
{ | |
"name": "firestore-bulk-delete", | |
"main": "index.js", | |
"dependencies": { | |
"firebase-admin": "^9.4.2" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment