[英]How to delete a collection with all its sub-collections and documents from Cloud Firestore
I have a Collection in Cloud Firestore and it has millions of documents and sub-collections.我在 Cloud Firestore 中有一个集合,它有数百万个文档和子集合。 I want to delete this collection with all its documents and sub-collection.
我想删除这个集合及其所有文档和子集合。 We can do this from Firebase console but that will take ages to delete this collection.
我们可以从 Firebase 控制台执行此操作,但删除此集合需要很长时间。
Is there any firebase cli command or node.js code snippet by using I can delete this collection?是否有任何 firebase cli 命令或 node.js 代码片段使用我可以删除此集合?
You can delete a collection by deleting all its documents.您可以通过删除其所有文档来删除集合。 You can read more in the official docs .
您可以在官方文档中阅读更多内容。 Example code here:
示例代码在这里:
async function deleteCollection(db, collectionPath, batchSize) {
const collectionRef = db.collection(collectionPath);
const query = collectionRef.orderBy('__name__').limit(batchSize);
return new Promise((resolve, reject) => {
deleteQueryBatch(db, query, resolve).catch(reject);
});
}
async function deleteQueryBatch(db, query, resolve) {
const snapshot = await query.get();
const batchSize = snapshot.size;
if (batchSize === 0) {
// When there are no documents left, we are done
resolve();
return;
}
// Delete documents in a batch
const batch = db.batch();
snapshot.docs.forEach((doc) => {
batch.delete(doc.ref);
});
await batch.commit();
// Recurse on the next process tick, to avoid
// exploding the stack.
process.nextTick(() => {
deleteQueryBatch(db, query, resolve);
});
}
In case the answer above is not working, you can use Cloud Functions as in here如果答案以上不工作,你可以使用云功能在这里
The Firebase CLI has a firestore:delete
command that recursively deletes content too. Firebase CLI 有一个
firestore:delete
命令,它也可以递归删除内容。 See its documentation here .在此处查看其文档。
Note that neither the API nor the CLI are likely to be significantly faster than the console.请注意,API 和 CLI 都不可能比控制台快得多。 Since there is no API to bulk-delete data, they all are essentially taking the same approach.
由于没有用于批量删除数据的 API,因此它们基本上都采用相同的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.