The GCR Garbage Collector
Tool for cleaning up images on the Google Container Registry. Initially based on the gist by Ahmet Alp Balkan, and rewritten in Go.
- Remove images older than the date specified with option
-date
- Keep images within a given retention period
-retention-period
- Clean up multiple image repositories at once with option
-all
- Exclude some image repositories with option
-exclude-repository
- Exclude images with certain tag(s) from deletion with option
-exclude-tag
- Exclude images with tags matching a regexp pattern with option
-exclude-tag-pattern
- Exclude images with tags matching a SemVer pattern with option
-exclude-semver-tags
Note: The SemVer standard does not include the
v
orV
prefix (e.g. v1.0.0), but as it is widely used, our Regexp will also match tags beginning with eitherv
orV
, so they will be excluded from deletion as well. - Only remove untagged images with
-untagged-only
flag - Dry-run mode with option
-dry-run
(don't actually delete images but get same output)
You need an authenticated local gcloud
installation, and write access to a Google Container Registry.
You can use a service account as well by setting the GOOGLE_APPLICATION_CREDENTIALS
environment variable. Read the Google documentation for more details.
- Download your desired version
- Extract it
tar xvf gcrgc_0.3.2_linux_amd64.tar.gz
- Move binary to desired destination
mv gcrgc /usr/local/bin
go get github.com/graillus/gcrgc
cd $GOPATH/src/github.com/graillus/gcrgc
go build -o bin/gcrgc cmd/gcrgc/gcrgc.go
A public image repository is available on DockerHub
docker pull graillus/gcrgc
Run with Google service account credentials:
docker run -t --rm \
-v /path/to/serviceaccount.json:/credentials \
-e GOOGLE_APPLICATION_CREDENTIALS=/credentials/serviceaccount.json
graillus/gcrgc ...
Clean up untagged images under the gcr.io/project-id/my-image
repository.
gcrgc -registry=gcr.io/project-id -untagged-only my-image
Clean up tagged and untagged images under the gcr.io/project-id/my-image
repository older than 2019-01-01, excluding tags master
and latest
gcrgc -registry=gcr.io/project-id -date=2019-01-01 -exclude-tag=latest -exclude-tag=master my-image
Clean up images older than 30 days
gcrgc -registry=gcr.io/project-id -retention-period 30d
Clean up tagged and untagged images under the gcr.io/project-id/my-image
excluding SemVer tags and latest
gcrgc -registry=gcr.io/project-id -exclude-tag=latest -exclude-semver-tags my-image
Clean up tagged and untagged images under the entire registry gcr.io/project-id
older than 2019-01-01, excluding the images under gcr.io/project-id/my-image
gcrgc -registry=gcr.io/project-id -all -date=2019-01-01 -exclude-repository=my-image
A Helm chart is available if you wish to run it on a Kubernetes cluster (as a CronJob
).
Check the documentation