@@ -41,18 +41,34 @@ function firestoreQueryParser(collectionRef, queryParams) {
41
41
} ;
42
42
43
43
function emptyCollection ( db , collection , callback ) {
44
+ var batchSize = 300 ;
44
45
var collectionRef = db . collection ( collection ) ;
45
- var query = collectionRef . get ( ) . then ( function ( querySnapshot ) {
46
- var writeBatch = db . batch ( ) ;
47
- querySnapshot . forEach ( function ( documentSnapshot ) {
48
- var documentPath = collection + '/' + documentSnapshot . id ;
49
- var documentRef = db . doc ( documentPath ) ;
50
- writeBatch . delete ( documentRef ) ;
51
- } ) ;
52
- writeBatch . commit ( ) . then ( function ( ) {
53
- if ( callback ) callback ( null ) ;
54
- } ) ;
55
- } ) ;
46
+ var query = collectionRef . limit ( batchSize ) ;
47
+
48
+ return deleteQueryBatch ( db , query , batchSize , callback ) ;
49
+ }
50
+
51
+ function deleteQueryBatch ( db , query , batchSize , callback ) {
52
+ query . get ( )
53
+ . then ( ( snapshot ) => {
54
+ if ( snapshot . size == 0 ) {
55
+ return 0 ;
56
+ }
57
+
58
+ var batch = db . batch ( ) ;
59
+ snapshot . docs . forEach ( ( doc ) => batch . delete ( doc . ref ) ) ;
60
+ return batch . commit ( ) . then ( ( ) => {
61
+ return snapshot . size ;
62
+ } ) ;
63
+ } ) . then ( ( numDeleted ) => {
64
+ if ( numDeleted == 0 ) {
65
+ return callback ( ) ;
66
+ }
67
+
68
+ process . nextTick ( ( ) => {
69
+ deleteQueryBatch ( db , query , batchSize , callback ) ;
70
+ } ) ;
71
+ } ) . catch ( callback ) ;
56
72
} ;
57
73
58
74
function getPrecondition ( vm ) {
0 commit comments