【问题标题】:Removing all documents and collections from the Firestore从 Firestore 中删除所有文档和集合
【发布时间】:2018-10-19 14:56:21
【问题描述】:

我正在尝试清除一个 Firestore 数据库,该数据库中包含大量文档和子集合以进行测试。 Firebase CLI (firebase-tools@3.18.4) 建议从 Cloud Firestore 中删除数据的可能性如下:

用法: firestore:delete [options] [path]

选项:

-r, --recursive    Recursive. Delete all documents and subcollections. Any action which would result in the deletion of child documents will fail if this argument is not passed. May not be passed along with --shallow.
--shallow          Shallow. Delete only parent documents and ignore documents in subcollections. Any action which would orphan documents will fail if this argument is not passed. May not be passed along with -r.
--all-collections  Delete all. Deletes the entire Firestore database, including all collections and documents. Any other flags or arguments will be ignored.
-y, --yes          No confirmation. Otherwise, a confirmation prompt will appear.

问题是它对我不起作用。

执行firebase firestore:delete --all-collections 会产生以下输出:

You are about to delete YOUR ENTIRE DATABASE. Are you sure? Yes
Deleting the following collections: 13OPlWrRit5PoaAbM0Rk, 17lHmJpTKVn1MBBbC169, 18LvlhhaCA1tygJYqIDt, 1DgDspzJwSEZrYxeM5G6, 1GQE7ySki4MhXxAeAzpx, 1MhoDe5JZY8Lz3yd7rVl, 1NOZ7OJeqSKl38dyh5Sw, 1Rxkjpgmr3gKvYhBJX29, 1S3mAhzQMd137Eli7qAp, 1S8FZxuefpIWBGx0hJW2, 1a7viEplYa79eNNus5xC, 1cgzMxAayzSkZv2iZf6e, 1dGjESrw6j12hEOqMpky, 1dbfgFD5teTXvQ6Ym897, 1eeYQgv2BJIS0aFWPksD, 1ehWNAZ0uKwg7mPXt3go, 1fDTkbwrXmGwZlFUl3zi, 1k5bk4aiMCuPw2KvCoAl, 1pxUSDh1YqkQAcuUH9Ie, 1rMSZ5Ru0cAfdcjY0Ljy
Deleted 92 docs (652 docs/s)

即使在多次执行该函数后,仍有大量文档和子集合保留在 Firestore 数据库中。不是删除ENTIRE DATABASE,而是在执行命令时只删除了大约70-150个文档。

如何删除整个数据库?

【问题讨论】:

标签: firebase google-cloud-firestore firebase-cli


【解决方案1】:

我已将此作为错误报告并收到以下答复:

目前,这是一种预期行为。正如我们的documentation 中所述,删除超过 500 个文档的集合需要多个批处理操作。因此,进行迭代将是处理部分删除情况的好方法。我还建议您查看我们的文档,了解有关某些可调用函数 limitations 的更多详细信息。

这意味着 firebase-tools 始终在一次操作中最多删除 500 个文档。我删除数据库中所有集合和文档的解决方案是使用 while 循环:

while firebase firestore:delete --all-collections --project MYPROJECT -y; do :; done

经过一些迭代后,您将看到没有任何集合,您可以停止脚本。您的 Firestore 数据库现在完全是空的。

【讨论】:

  • 您是否尝试过.set() 使用空对象?我知道这可以实时删除整个数据库。
  • 嘿@RonRoyston,不,这不起作用。在 Firestore 中,每个文档都可能有子集合 - 即使文档被删除,子集合仍然存在。见firebase.google.com/docs/firestore/manage-data/…
【解决方案2】:

您需要使用admin sdk 来完成此任务。使用 .listDocuments.listCollections 构建简单的迭代来执行您的 .delete 操作。

如果文档.listCollections 响应的长度为零或null 或为空,则您知道没有子集合并且可以迭代/跳过。否则,迭代该子集合文档以查找要删除的更深层次的子集合。

let documentRef = firestore.doc('col/doc');

documentRef.listCollections().then(collections => {
  for (let collection of collections) {
    console.log(`Found subcollection with id: ${collection.id}`);
  }
});

let collectionRef = firestore.collection('col');

return collectionRef.listDocuments().then(documentRefs => {
   return firestore.getAll(documentRefs);
}).then(documentSnapshots => {
   for (let documentSnapshot of documentSnapshots) {
      if (documentSnapshot.exists) {
        console.log(`Found document with data: ${documentSnapshot.id}`);
      } else {
        console.log(`Found missing document: ${documentSnapshot.id}`);
      }
   }
});

【讨论】:

    猜你喜欢
    • 2020-09-08
    • 2018-11-01
    • 2021-01-08
    • 1970-01-01
    • 2023-03-24
    • 2018-08-23
    相关资源
    最近更新 更多