GCS CLI: using “gsutil rm” to remove files by creation date

3k views Asked by At

Is there a way to remove files from GoogleCloudStorage, using the CLI by their creation date?

For example: I would like to remove all files in a specific path, which their creation date is lower than 2016-12-01

1

There are 1 answers

0
Travis Hobrla On

There's no built-in way in the CLI to delete by date. There are a couple ways to accomplish something like this. One possibility is to use an object naming scheme that prefixes object names by their creation date. Then it is easy to remove them with wildcards, for example:

gsutil -m rm gs://your-bucket/2016-12-01/*

Another approach would be to write a short parser for gsutil ls -L gs://your-bucket that filters object names by their creation date, then call gsutil -m rm -I with the resulting object names.

If you just want to automatically delete objects older than a certain age, then there is a much easier way than using the CLI: you can configure an Object Lifecycle Management policy on your bucket.