简体   繁体   中英

How to delete every collections from 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?

在此处输入图像描述

Wipe your firestore database from the command line in one go:

firebase firestore:delete --all-collections -y

There is no action in the Firebase console, nor an API, to delete all collections in one go.

If you need to delete all collections, you will either have to delete them one-by-one in the console, or indeed script the deletion by calling the API repeatedly.

Here is what I did:

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

USAGE :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM