Search code examples
firebasegoogle-cloud-platformgoogle-cloud-functionsgoogle-cloud-storagefirebase-storage

Determine whether a file was deleted or overwritten - Firebase Storage OnDelete()


Is there a way to determine whether a file was truly deleted, or whether it was updated (overwritten) when OnDelete() is triggered, other than calling file.exists()?

OnDelete() is known to be triggered when a file was deleted or overwritten - this is an expected behavior, mentioned in the documentation.

I use it in my app when a user deletes his profile picture. So when triggered, I can't tell whether the picture was deleted or updated, because it'd be triggered in both cases. So I have to manually check what happened, and this is exactly my problem.

I can obviously use file.exists() for the job, but I consider it an expensive call (or am I wrong?) because this is accessing the storage again, and it seems like there must be a way to avoid it, but I'm not sure.

If there is no way, can I safely assume that exists() will never precede the overwrite event, causing a false negative? (I'm worried it would operate prior the overwrite completion, find no existing file, and return false).


Solution

  • According to the documentation, if you use a versioning bucket, OnDelete() is only triggered "when a version is permanently deleted (but not when an object is archived)".

    In order to transform your bucket to a versioning one, follow this documentation: https://cloud.google.com/storage/docs/using-object-versioning#enable