Oleg Novosad
Oleg Novosad

Reputation: 2421

Firebase Extensions: delete user data in sub-collections

I don't know where to ask and I was not able to find that in documentation. I am using Delete User Data extension: https://extensions.dev/extensions/firebase/delete-user-data.

From the docs and everywhere they write an example for deleting from multiple collections like: users/{UID}, admins/{UID} etc. I am curious how can I delete this user by id from subcollections of other users. Here is the use case for two user IDs foo and bar:

User with id foo:

/users/foo/followers/bar

User with id bar:

/users/bar/following/foo

I want to delete user foo from the following sub-collection of user bar.

I've turned on recursive delete with depth 3. I've tried specifying collections like users/{otherUid}/followers/{UID} in the extension configuration - no luck.

I'd very like to hear input from the Firebase team here. Thanks in advance!

Example image (don't pay much attention to IDs themselves, its just a sample structure):

My expectations are that if I delete user with id 424596510 (from Firebase Authentication) - it will be deleted the highlighted users followers collection.

enter image description here

Upvotes: 0

Views: 69

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138804

From the docs and everywhere they write an example for deleting from multiple collections like users/{UID}, admins/{UID}.

Yes, if the user data exists in multiple collections, for example, users and admins, and each document ID is represented by the UID of the user that comes from the Authentication process, then you can add multiple paths separated by commas, users/{UID},admins/{UID}.

This basically means that you can delete the data that is associated with a user from multiple Firestore collections.

I am curious how can I delete this user by ID from subcollections of other users.

This Firebase Extension allows you to delete the data that is associated with a user and additional data that exists within other sub-collections. To achieve this, you have set the "Enable autodiscovery" to "Yes" which:

Works by automatically traversing the database to find collections and documents that should be deleted according to your configuration.

So in your case, the 3) b. it will apply, which:

Finally, for each document: a. If the current search depth (see below) is less than or equal to the configured search depth, the process will be repeated for all of the current document’s sub-collections.

So this Firebase Extension will help you delete user documents that exist in sub-collections and not only in root collections. It will also delete documents that have a field set to the UID.

What will no do:

It will NOT automatically delete UIDs stored in arrays or maps, and it will not search for data keyed by user ID stored in deeply nested subcollections past the depth specified above.

Upvotes: 0

Related Questions