Brejuro
Brejuro

Reputation: 3541

Firestore delete all documents in a subcollection

In my Cloud Firestore setup (iOS/Swift app), I have a Post collection with a Likes subcollection. The Likes subcollection has a document that contains the information of the user that liked that post, with the documentID being the userID of the liker.

When the user updates their post, I want to clear the Likes subcollection (in my app, the user just updates a post instead of creating a new one).

I have read that deleting every document in a subcollection is not advised, so I am wondering how I can accomplish this. A popular user could have thousands and thousands of likes, so deleting them all could be costly.

It's my understanding that I can accomplish this somehow using Cloud Functions? I tried reading up on Cloud Functions and I'm getting pretty lost. Is there a way I can trigger a Cloud Function to delete all the documents in the Likes subcollection for that user upon updating their post?

Upvotes: 1

Views: 4339

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

From Cloud Functions you're just using the Firebase Admin SDK to access Firestore. There is no magic extra APIs that you get from running that SDK in Cloud Functions.

There is no way to delete all documents from a collection in one go, neither in the client-side SDKs nor in the Admin SDK. For more on this, see:

Upvotes: 1

Related Questions