How to delete every collections from firestore

时间:2018-08-31 16:13:07

标签: firebase google-cloud-firestore

I cannot find a delete button in order to erase all collections on fireStore using the Firebase console at once. I can only remove the collections one by one.

Is there a way to delete everything from firebase console/import data from Json (like the firebase database) in FireStore or I have to write a script for that?

enter image description here

之间

3 个答案:

答案 0 :(得分:1)

通过命令行一次性擦除Firestore数据库:

firebase firestore:delete --all-collections -y

答案 1 :(得分:0)

Firebase控制台中没有操作,也没有API可以一次性删除所有集合。

如果您需要删除所有集合,则必须在控制台中一一删除它们,或者确实可以通过重复调用API来对删除进行脚本编写。

答案 2 :(得分:0)

这是我所做的:

   deleteCollection(db, collectionPath, batchSize) {
        let collectionRef = db.collection(collectionPath);
        let query = collectionRef.orderBy('__name__').limit(batchSize);

        return new Promise((resolve, reject) => {
            this.deleteQueryBatch(db, query, batchSize, resolve, reject);
        });
    }

    deleteQueryBatch(db, query, batchSize, resolve, reject) {
        query.get()
            .then((snapshot) => {
                // When there are no documents left, we are done
                if (snapshot.size === 0) {
                    return 0;
                }

                // Delete documents in a batch
                let batch = db.batch();
                snapshot.docs.forEach((doc) => {
                    batch.delete(doc.ref);
                });

                return batch.commit().then(() => {
                    return snapshot.size;
                });
            }).then((numDeleted) => {
            if (numDeleted === 0) {
                resolve();
                return;
            }

            // Recurse on the next process tick, to avoid
            // exploding the stack.
            process.nextTick(() => {
                this.deleteQueryBatch(db, query, batchSize, resolve, reject);
            });
        })
            .catch(reject);
    }

用法

flushDB() {
    this.deleteCollection(db, 'users', 100)
    this.deleteCollection(db, 'featureFlags', 100)
    this.deleteCollection(db, 'preferences', 100)
}