Skip to content

Commit 8dd74a4

Browse files
authored
feat: use native recursiveDelete for collection cleanup (#408)
* feat: use native recursiveDelete for collection cleanup * chore: revert package.json to original state as requested
1 parent 4738eab commit 8dd74a4

File tree

1 file changed

+5
-33
lines changed

1 file changed

+5
-33
lines changed

firestore/main/index.js

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -947,37 +947,9 @@ async function multipleCursorConditions(db) {
947947
}
948948

949949
// [START firestore_data_delete_collection]
950-
async function deleteCollection(db, collectionPath, batchSize) {
950+
async function deleteCollection(db, collectionPath) {
951951
const collectionRef = db.collection(collectionPath);
952-
const query = collectionRef.orderBy('__name__').limit(batchSize);
953-
954-
return new Promise((resolve, reject) => {
955-
deleteQueryBatch(db, query, resolve).catch(reject);
956-
});
957-
}
958-
959-
async function deleteQueryBatch(db, query, resolve) {
960-
const snapshot = await query.get();
961-
962-
const batchSize = snapshot.size;
963-
if (batchSize === 0) {
964-
// When there are no documents left, we are done
965-
resolve();
966-
return;
967-
}
968-
969-
// Delete documents in a batch
970-
const batch = db.batch();
971-
snapshot.docs.forEach((doc) => {
972-
batch.delete(doc.ref);
973-
});
974-
await batch.commit();
975-
976-
// Recurse on the next process tick, to avoid
977-
// exploding the stack.
978-
process.nextTick(() => {
979-
deleteQueryBatch(db, query, resolve);
980-
});
952+
return await db.recursiveDelete(collectionRef);
981953
}
982954

983955
// [END firestore_data_delete_collection]
@@ -1019,7 +991,7 @@ describe('Firestore Smoketests', () => {
1019991
});
1020992

1021993
it('should delete existing documents', () => {
1022-
return deleteCollection(db, 'cities', 50);
994+
return deleteCollection(db, 'cities');
1023995
});
1024996

1025997
it('should store example data', () => {
@@ -1081,7 +1053,7 @@ describe('Firestore Smoketests', () => {
10811053
it('should handle transaction with a result', () => {
10821054
return transactionWithResult(db).then(res => {
10831055
// Delete data set
1084-
return deleteCollection(db, 'cities', 50);
1056+
return deleteCollection(db, 'cities');
10851057
});
10861058
});
10871059

@@ -1178,7 +1150,7 @@ describe('Firestore Smoketests', () => {
11781150
});
11791151

11801152
it('should delete the whole collection', () => {
1181-
return deleteCollection(db, 'cities', 50);
1153+
return deleteCollection(db, 'cities');
11821154
});
11831155

11841156
it('should find all museums when querying a collection group', () => {

0 commit comments

Comments
 (0)