How can I see the list of orphaned collections

680 views
Skip to first unread message

Ruben Gonzalez Torres

unread,
Mar 5, 2019, 9:56:47 AM3/5/19
to Firebase Google Group
Hi,

I have been using Firebase Real Time DB for the last few years and now I am doing my first project with Firestore. I was testing and I deleted a few documents assuming it will delete all sub-collections automatically. Now, my mobile app is receiving a callback with documents from an orphaned sub-collection and then I read sub-collections aren't delete automatically. I read about the trade-off and why it was built that way.
My question is how can I see all the orphaned collections and documents? I have been deleted documents without removing sub-collections. So, I am sure, I have a lot of orphaned collections that I am not able to see in the console right now. Is there any way to see all my orphaned collections?

Best

Kato Richardson

unread,
Mar 13, 2019, 11:52:31 AM3/13/19
to Firebase Google Group
Hi Ruben,

Sorry we missed this post. Looks like it's covered in this doc and discussed here on Stack Overflow.

From an admin SDK:
var sfRef = db.collection('cities').doc('SF');
sfRef
.getCollections().then(collections => {
  collections
.forEach(collection => {
    console
.log('Found subcollection with id:', collection.id);
 
});
});

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/83055a88-dcdf-4dbf-b1a3-b57d4e40793f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--

Kato Richardson | Developer Programs Eng | kato...@google.com | 775-235-8398

Ruben Gonzalez Torres

unread,
Mar 14, 2019, 10:02:47 AM3/14/19
to Firebase Google Group
Thanks for your reply Kato, but I don't know the id of the parent document. The simplified version of my DB is: users -> {userId} -> accounts -> {accountId} -> AccountInfo. (userId and accountId are autogenerated by firebase)

I deleted a few users without removing accounts subcollection first, now I have the accounts living in the DB without a parent document. As some people called on other questions from this group "orphaned collections". How can I access those collections if I don't know the {userId}? I don't want to have data in my database that I am not using. Is there any way to see how many orphaned collections my db has?

Kato Richardson

unread,
Mar 14, 2019, 12:17:55 PM3/14/19
to Firebase Google Group
Ah, my apologies. I pattern matched this against the wrong question : / Sorry for not giving this better attention. After a cursory search, I don't see a good doc on this. But it looks possible given that you can list and delete them in the admin console even if the doc doesn't exist.

Let me query the gurus and see what best practices are here.

☼, Kato


For more options, visit https://groups.google.com/d/optout.

Kato Richardson

unread,
Mar 14, 2019, 12:43:52 PM3/14/19
to Firebase Google Group
Okay! 

Firestore superstar Gil pointed me to the Admin SDK, which has listDocuments(). Here's the description:

The document references returned may include references to "missing documents", i.e. document locations that have no document present but which contain subcollections with documents. Attempting to read such a document reference (e.g. via .get() or .onSnapshot()) will return a DocumentSnapshot whose .exists property is false.

Looks like that should do the job?

☼, Kato

Samuel Stern

unread,
Mar 14, 2019, 1:44:24 PM3/14/19
to Firebase Google Group
Hey Ruben,

The short answer is that there is no good way to do this.  The "orphaned" state of a doc is mostly a concept of the console UI and not something the backend is concerned with.  However there is a trick in the REST API you can use to query all documents below a certain path even without knowing their parent or depth.  You can see how we use this trick in the Firebase CLI's delete code:

If you really do want to "audit" your database and search for missing documents, playing with this API is probably your best bet.  I wish we had a simpler answer!

- Sam

Samuel Stern

unread,
Mar 14, 2019, 1:45:31 PM3/14/19
to Firebase Google Group
Well ... today we learned something together!  Use Gil's answer, not mine, if you know the general depth of the documents.

- Sam

Ruben Gonzalez Torres

unread,
Mar 14, 2019, 11:25:01 PM3/14/19
to Firebase Google Group
Okay, thanks. I can use listDocuments()
Reply all
Reply to author
Forward
0 new messages