0

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

WJA
  • 6,676
  • 16
  • 85
  • 152
  • So you want to delete the entire `root` collection with all documents within it? – Alex Mamo Apr 12 '22 at 12:29
  • That is correct – WJA Apr 12 '22 at 12:40
  • You have to delete all documents within the collection, and in the end, the collection will disappear automatically. Give it a try and tell me if it works. – Alex Mamo Apr 12 '22 at 12:52
  • Yes I got that, but how do you do that recursively. What if you have 10k documents. – WJA Apr 12 '22 at 12:53
  • I think that this [answer](https://stackoverflow.com/questions/49125183/how-to-model-this-structure-to-handle-delete/49125226) might help. It's written in Java but I'm sure you can find something similar in Python. – Alex Mamo Apr 12 '22 at 13:06

1 Answers1

1

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
WJA
  • 6,676
  • 16
  • 85
  • 152