[英]How to delete every collections from firestore
從命令行一次性擦除您的 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.