从 Firestore 中删除所有文档和集合
Removing all documents and collections from the Firestore
我正在尝试清除一个 Firestore 数据库,该数据库中包含大量用于测试目的的文档和子集合。 Firebase CLI (firebase-tools@3.18.4) 建议从 Cloud Firestore 中删除数据的以下可能性:
用法: firestore:delete [选项] [路径]
选项:
-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 个文档。
如何删除整个数据库?
我已将此作为错误报告并收到以下答复:
Currently, this is an intended behavior. As stated in our documentation, deleting a collection of more than 500 documents requires multiple batched operations. So doing the iteration would be a good way to handle cases of partial deletion. I would also suggest that you check our docs regarding some of the callable function limitations for more details.
这意味着 firebase-tools 在一次操作中始终最多删除 500 个文档。我删除数据库中所有集合和文档的解决方案是使用 while 循环:
while firebase firestore:delete --all-collections --project MYPROJECT -y; do :; done
经过一些迭代后,您将看到没有剩余的集合,您可以停止脚本。您的 Firestore 数据库现在完全是空的。
您将希望使用 admin sdk for this task. Use .listDocuments
and .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}`);
}
}
});
我正在尝试清除一个 Firestore 数据库,该数据库中包含大量用于测试目的的文档和子集合。 Firebase CLI (firebase-tools@3.18.4) 建议从 Cloud Firestore 中删除数据的以下可能性:
用法: firestore:delete [选项] [路径]
选项:
-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 个文档。
如何删除整个数据库?
我已将此作为错误报告并收到以下答复:
Currently, this is an intended behavior. As stated in our documentation, deleting a collection of more than 500 documents requires multiple batched operations. So doing the iteration would be a good way to handle cases of partial deletion. I would also suggest that you check our docs regarding some of the callable function limitations for more details.
这意味着 firebase-tools 在一次操作中始终最多删除 500 个文档。我删除数据库中所有集合和文档的解决方案是使用 while 循环:
while firebase firestore:delete --all-collections --project MYPROJECT -y; do :; done
经过一些迭代后,您将看到没有剩余的集合,您可以停止脚本。您的 Firestore 数据库现在完全是空的。
您将希望使用 admin sdk for this task. Use .listDocuments
and .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}`); } } });