Deleting all documents in Firestore collection

后端 未结 7 866
我在风中等你
我在风中等你 2020-11-30 06:51

I\'m looking for a way to clear an entire collection. I saw that there is a batch update option, but that would require me to know all of the document IDs in the collection.

7条回答
  •  时光取名叫无心
    2020-11-30 07:10

    You have to get all the documents then use batch to delete them in bulk P.S. i prefer try...catch syntax

        let deleteInBatch = async (query, size = 100) => {
        try{
    
            let batch = firestore().batch();
    
            //get documents
            let values = await query.get();
            if(values.size>0){
                values.foreach(value=> {
                    batch.delete(value.ref);
                })
    
                //Delete the documents in bulk
                batch.commit();
                if(values.size>0){
                    //Recusively call the function again to finish
                    //deleting the rest of documents
                    deleteInBatch(query,size);
                }
            }else{
                //exist function
                return;
            }
        }catch(err){
            throw err;
        }
    }
    

提交回复
热议问题