简体   繁体   English

如何从 Cloud Firestore 删除集合及其所有子集合和文档

[英]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.

相关问题 谷歌云 Function 删除“丢失文档”上的子集合 - Google Cloud Function to delete sub-collections on "missing documents 如何访问firestore中单个集合中多个文档的子集合? - How to access sub-collections across multiple documents within a single collection in firestore? 在 Firestore 中获取子集合的文档 - Fetching documents of sub-collections in firestore Firestore 如何从 collections 中提取子集合 - Firestore how to extract sub collection from collections 如何在 Cloud Firestore 中有效地获取集合及其子集合的所有文档 - How to fetch all the documents of a collection and it's sub collection efficiently in Cloud Firestore 如何删除firestore集合数据库中的所有文档 - How to delete all the documents in a firestore collection database 在子集合更新时触发云 function - Trigger cloud function on sub-collections update 如何在Cloud Firestore(Web)中获取所有不带名称的集合和文档 - How to get all collections and documents without their names in Cloud Firestore (web) 如何在 Firebase Cloud Firestore 中删除具有相同键值的集合中的文档 - How to delete documents in a collection with the same key value in Firebase Cloud Firestore 如何在一个“单击”事件中同时从我的云 Firestore 中的两个集合中删除两个文档? - How can I simultaneously in one "click" event delete two documents from two collection in my cloud firestore?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM