Search code examples
pythongoogle-cloud-firestoredocument-database

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?

enter image description here


Solution

  • 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