import { collection, query, orderBy, limit, getDocs, writeBatch } from "firebase/firestore";
function deleteCollection(db, collectionRef, batchSize) {
const q = query(collectionRef, orderBy('__name__'), limit(batchSize));
return new Promise((resolve) => {
deleteQueryBatch(db, q, batchSize, resolve);
});
}
async function deleteQueryBatch(db, query, batchSize, resolve) {
const snapshot = await getDocs(query);
let numDeleted = 0;
if (snapshot.size > 0) {
const batch = writeBatch(db);
snapshot.docs.forEach((doc) => {
batch.delete(doc.ref);
});
await batch.commit();
numDeleted = snapshot.size;
}
if (numDeleted < batchSize) {
resolve();
return;
}
setTimeout(() => {
deleteQueryBatch(db, query, batchSize, resolve);
}, 0);
}