FR: Add File.delete options property to count missing file as success
See original GitHub issueIs your feature request related to a problem? Please describe.
I frequently find myself performing delete
operations on files that may or may not exist. Similar to how Cloud Firestore’s delete
operation works, I would like for the Cloud Storage delete
operation to consider a non-existent file as a successful deletion. In other words, I want my delete
operation to be considered successful if the file does not exist after the operation completes- regardless of if it did exist before the operation.
Describe the solution you’d like
The delete
operation (docs here) takes an optional options
object, whose only parameter is currently userProject
. I would like a new property to be available that I could use to enable this mechanism on a per-operation basis. The syntax might look something like:
await myBucket.file("thisFileMightNotExist.txt").delete({mustExist: false})
Describe alternatives you’ve considered
The two alternatives are to either 1) run a File.exists
operation first, or 2) handle deletion error in catch block. Both alternatives work, they’re just not very elegant. Alternative 1 technically also leaves an unlikely but possible scenario where the file is removed after the the exists
operation but before the delete
operation.
Additional context None.
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (5 by maintainers)
@willbattel thank you for the suggestion! 5.6.0 is out with the feature.
This was released upstream, and here is a PR to officially introduce it here: #1347