Handle gsutil ls and rm command errors if no files present

Try this:

gsutil -m rm gs://mybucket/foo/* 2> /dev/null || true


gsutil -m ls gs://mybucket/foo/* 2> /dev/null || true

This has the effect of suppressing stderr (it's directed to /dev/null), and returning a success error code even on failure.

You might not want to ignore all errors as it might indicate something different that file not found. With the following script you'll ignore only the 'One or more URLs matched not objects' but will inform you of a different error. And if there is no error it will just delete the file:

gsutil -m rm gs://mybucket/subbucket/* 2> temp
if [ $? == 1 ]; then
    grep 'One or more URLs matched no objects' temp
    if [ $? == 0 ]; then
        echo "no such file"
        echo temp
rm temp

This will pipe stderr to a temp file and will check the message to decide whether to ignore it or show it.

And it also works for single file deletions. I hope it helps.


How to grep standard error stream
Bash Reference Manual - Redirections