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

5
  • So you want to delete the entire root collection with all documents within it? Commented Apr 12, 2022 at 12:29
  • That is correct Commented Apr 12, 2022 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. Commented Apr 12, 2022 at 12:52
  • Yes I got that, but how do you do that recursively. What if you have 10k documents. Commented Apr 12, 2022 at 12:53
  • I think that this answer might help. It's written in Java but I'm sure you can find something similar in Python. Commented Apr 12, 2022 at 13:06

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.