1

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?

Brejuro
  • 3,421
  • 8
  • 34
  • 61

1 Answers1

1

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:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • I understand that there's no function for deleting a collection in the Admin SDK, but is it a better idea to do it from a cloud function for performance reasons? I'm just trying to figure out how I would go about clearing my Likes subcollection in the best way possible. Or is this something I should just try to structure better in my database – Brejuro May 22 '18 at 20:38
  • There is no definitive better. See the third link on a reason to do it in Cloud Functions. – Frank van Puffelen May 22 '18 at 20:49