6

I am trying to clear a Firestore database which was filled with a lot of documents and subcollections for testing purposes. The Firebase CLI (firebase-tools@3.18.4) suggests the following possibility to delete data from Cloud Firestore:

Usage: firestore:delete [options] [path]

Options:

-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.

The problem is that it does not really work for me.

Executing firebase firestore:delete --all-collections produces the following output:

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)

Even after executing the function multiple times an awful lot of documents and subcollections still remain in the Firestore database. Instead of deleting the ENTIRE DATABASE, only about 70-150 documents are deleted when the command is executed.

How can the entire database be deleted?

4

2 回答 2

2

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

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

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

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

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

于 2019-02-16T10:53:17.147 回答
0

您将需要使用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}`);
      }
   }
});
于 2019-02-25T17:17:38.707 回答