问题
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?
回答1:
Wipe your firestore database from the command line in one go:
firebase firestore:delete --all-collections -y
回答2:
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.
回答3:
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)
}
来源:https://stackoverflow.com/questions/52119301/how-to-delete-every-collections-from-firestore