Deleting all documents in Firestore collection

后端 未结 7 874
我在风中等你
我在风中等你 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:08

    The following javascript function will delete any collection:

    deleteCollection(path) {
        firebase.firestore().collection(path).listDocuments().then(val => {
            val.map((val) => {
                val.delete()
            })
        })
    }
    

    This works by iterating through every document and deleting each.

    Alternatively, you can make use of Firestore's batch commands and delete all at once using the following function:

    deleteCollection(path) {
        // Get a new write batch
        var batch = firebase.firestore().batch()
    
        firebase.firestore().collection(path).listDocuments().then(val => {
            val.map((val) => {
                batch.delete(val)
            })
    
            batch.commit()
        })
    }
    

提交回复
热议问题