Deleting all documents in Firestore collection

后端 未结 7 815
我在风中等你
我在风中等你 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 06:54

    Tested in VueJS

    import db from '@/firebase/init' 
    
    let ref = db.collection('YOUR_COLLECTION_NAME')
    
    db.collection(path).onSnapshot(snapshot => {
        snapshot.docs.forEach(doc => {
            ref.doc(doc.id).delete()
            .catch(error => {
                console.log(error)
            })
        })
    })
    
    
    0 讨论(0)
  • 2020-11-30 07:06

    There is no API to delete an entire collection (or its contents) in one go.

    From the Firestore documentation:

    To delete an entire collection or subcollection in Cloud Firestore, retrieve all the documents within the collection or subcollection and delete them. If you have larger collections, you may want to delete the documents in smaller batches to avoid out-of-memory errors. Repeat the process until you've deleted the entire collection or subcollection.

    There is even a Swift sample in that documentation, so I recommend you try it.

    The Firebase CLI allows you to delete an entire collection with a single command, but it just calls the API to delete all documents in that collection in batches. If this suits your needs, I recommend you check out the (sparse) documentation for the firestore:delete command.

    0 讨论(0)
  • 2020-11-30 07:06

    There is now an option in the firebase CLI to delete an entire firestore database:

    firebase firestore:delete --all-collections
    
    0 讨论(0)
  • 2020-11-30 07:06

    The cleanest way I have found to delete all documents. The only time I would use this function is when using the emulator and you can simply paste the function into the console:

    // Paste this in:
    function clearCollection(path) {
      const ref = firestore.collection(path)
      ref.onSnapshot((snapshot) => {
        snapshot.docs.forEach((doc) => {
          ref.doc(doc.id).delete()
        })
      })
    }
    // Use it like this:
    clearCollection('layers')
    

    If you find yourself needing this code repeatedly save it as a snippet in Chrome and then you can have easy access to it and won't have to keep pasting the code block into the console. You must run the snippet before it is accessible from the code block. Documentation

    0 讨论(0)
  • 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()
        })
    }
    
    0 讨论(0)
  • 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;
        }
    }
    
    0 讨论(0)
提交回复
热议问题