简体   繁体   中英

How to delete nested collections from an empty document in Firestore?

I have the following structure for one of my collections on Firestore:

main_database/root/comments/{commentId}/message: str
main_database/root/comments/{commentId}/replies/{replyId}/message: str

I tried to remove all documents from the root by deleting the root document:

alerts_database.delete_document(document_path=["main_database", "root"])

...

def delete_document(self, document_path: List[str]) -> None:
    document_reference = self.get_document_reference_from_path(path=document_path)
    collection_list = self.get_document_collection_list(document_reference=document_reference)
    for collection in collection_list:
        self.delete_collection(collection_reference=collection)
    logger.warning(f"Deleting document: {document_path}.")
    document_reference.delete()

However, this only deleted all the documents but not the replies. As such I am stuck with replies in an empty document.

How can I flush the database at the main_database/root level?

在此处输入图像描述

After some digging, I found that you can use the firebase-cli with the --recursive tag to recursively delete the entire collection and subcollections as follows:

firebase firestore:delete "foo/bar" --recursive --project=PROJECT_ID

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM