簡體   English   中英

如何從 firestore 中刪除每個集合

[英]How to delete every collections from firestore

我找不到刪除按鈕,無法使用 Firebase 控制台一次刪除 fireStore 上的所有集合。 我只能一個一個地刪除集合。

有沒有辦法從 Firebase 控制台刪除所有內容/從 FireStore 中的 Json(如 firebase 數據庫)導入數據,或者我必須為此編寫腳本?

在此處輸入圖像描述

從命令行一次性擦除您的 firestore 數據庫:

firebase firestore:delete --all-collections -y

Firebase 控制台中沒有任何操作,也沒有 API 可以一次性刪除所有集合。

如果您需要刪除所有集合,您要么必須在控制台中逐個刪除它們,要么通過重復調用 API 編寫刪除腳本。

這是我所做的:

   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)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM