12

I'm looking for a best practice on combined/batch calls for Firebase Storage and Firebase Firestore. For example: I need to delete a document from my Firestore which is related to an image in Firebase Storage. Is there a possibility to do this in such a way that when one of the actions fails, the whole transaction is being reverted?

Peter
  • 10,910
  • 3
  • 35
  • 68

2 Answers2

3

There is no real atomic/transactional way of doing that however you can use cloud functions to achieve it reliably.

You can delete a document, and use onDelete trigger to delete the actual file in the storage.

So when you can't delete the document, nothing will happen to the file. If you succeed to delete the document, cloud function will be triggered to delete the image.

abeyaz
  • 3,034
  • 1
  • 16
  • 20
  • 2
    To anyone reading this in 2021... This won't guarantee the image is deleted. If the cloud function fails to delete the image, you will have deleted the document but not the image. The goal is for both actions to fail/succeed together. This does not accomplish that goal. – nth-child Sep 28 '21 at 20:37
1

None of the Firebase products support such cross-product transactional updates. You'll have to nest the calls during updates, and use code that can handle unexpected data structures during reads.

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807