I'm trying to clean up my Google Cloud Firestore database, and I have some subcollections with no parent doc (the parent was deleted). How can I find all of those, using the Firebase Admin SDK, so I can delete them?
1 Answers
You will end up writing a lot of code for this. I'm going to link to nodejs APIs.
For each collection where there could be missing documents, you will need to need to query that collection with listDocuments(). That will return a list of all documents in the collection, including the missing documents that have subcollections. You will then need to iterate the DocumentReferences returned in that list, and call get() on every one of them. The returned DocumentSnapshot will then tell you if it exists or not using its exists
property.
After you have all the DocumentReference objects referring to missing documents, you can then follow the instructions in this other question that describes how to delete all nested subcollections under that DocumentReference, go straight to the Firebase documentation.

- 297,357
- 32
- 422
- 441
-
Thanks -- I'll try this. Doesn't sound too bad actually. I didn't realize that `listDocuments` would return "missing" docs; that's good. I already have the deep document/collection deleter from https://github.com/firebase/firebase-admin-node/issues/361. – GaryO Aug 27 '20 at 22:41