diff --git a/firestore/main/index.js b/firestore/main/index.js index 517a7394..6e5b910e 100644 --- a/firestore/main/index.js +++ b/firestore/main/index.js @@ -947,37 +947,9 @@ async function multipleCursorConditions(db) { } // [START firestore_data_delete_collection] -async function deleteCollection(db, collectionPath, batchSize) { +async function deleteCollection(db, collectionPath) { const collectionRef = db.collection(collectionPath); - const query = collectionRef.orderBy('__name__').limit(batchSize); - - return new Promise((resolve, reject) => { - deleteQueryBatch(db, query, resolve).catch(reject); - }); -} - -async function deleteQueryBatch(db, query, resolve) { - const snapshot = await query.get(); - - const batchSize = snapshot.size; - if (batchSize === 0) { - // When there are no documents left, we are done - resolve(); - return; - } - - // Delete documents in a batch - const batch = db.batch(); - snapshot.docs.forEach((doc) => { - batch.delete(doc.ref); - }); - await batch.commit(); - - // Recurse on the next process tick, to avoid - // exploding the stack. - process.nextTick(() => { - deleteQueryBatch(db, query, resolve); - }); + return await db.recursiveDelete(collectionRef); } // [END firestore_data_delete_collection] @@ -1019,7 +991,7 @@ describe('Firestore Smoketests', () => { }); it('should delete existing documents', () => { - return deleteCollection(db, 'cities', 50); + return deleteCollection(db, 'cities'); }); it('should store example data', () => { @@ -1081,7 +1053,7 @@ describe('Firestore Smoketests', () => { it('should handle transaction with a result', () => { return transactionWithResult(db).then(res => { // Delete data set - return deleteCollection(db, 'cities', 50); + return deleteCollection(db, 'cities'); }); }); @@ -1178,7 +1150,7 @@ describe('Firestore Smoketests', () => { }); it('should delete the whole collection', () => { - return deleteCollection(db, 'cities', 50); + return deleteCollection(db, 'cities'); }); it('should find all museums when querying a collection group', () => {