Search code examples
cmdgoogle-cloud-storagegsutil

How to delete objects with gsutils following a wildcard pattern?


We can run a gsutils command to delete objects as follows:

gsutil rm -a gs://bucket/**

Now I would like to only remove certain folders following a wildcard pattern:

gs://bucket/folder/{WILDCARD-A}/folderA/{WILDCARD-B}/folderB

The idea is to delete all files with target folder B.

How can this be achieved with gsutils?


Solution

  • Try gsutil rm -r gs://bucket/folder/{WILDCARD-A}/folderA/{WILDCARD-B}/folderB applying your wildcard patterns. If you have a large number of objects to remove, use the gsutil -m option, which enables multi-threading/multi-processing. In case you need to bulk delete a hundred thousand or more objects, avoid using gsutil, as the process takes a long time to complete. Instead use the Google Cloud console, which can delete up to several million objects, or Object Lifecycle Management, which can delete any number of objects.