I’m using Firebase cloud database and firebase storage. I’m using the following code in order to remove two documents using WriteBatch
:
WriteBatch batch = fireDB.batch(); batch.delete(docRef1); batch.delete(docRef2); batch.commit().addOnCompleteListener(new OnCompleteListener<Void>() { @Override public void onComplete(@NonNull Task<Void> task) { // code } });
I want also to remove the image from the storage using WriteBatch
, meaning, if one of them fails, they all fail to remove. I have a variable called imageURL
which contains the URL of the image in the firebase storage. I tried:
batch.delete(storage.getStorage().getReferenceFromUrl(imageURL));
But it does not work because:
‘delete(com.google.firebase.firestore.DocumentReference)’ in ‘com.google.firebase.firestore.WriteBatch’ cannot be applied to ‘(com.google.firebase.storage.StorageReference)’
Is it possible to do?
Advertisement
Answer
There is no way to run a single operation across multiple Firebase products.
The best you can do is:
- Perform the deletes in the order that leads to the least problems for your use-case. Typically this means deleting the images last, as having an orphaned image is less disruptive for an app than having a dangling reference to a non-existing image.
- Write robust code for the reading, that can deal with both orphaned images, and dangling references.
- Perform periodic cleanup, typically in a Cloud Function, getting rid of both orphaned files and dangling references.