When pushing images to Amazon ECR, if the tag already exists within the repo the old image remains within the registry but goes in an untagged state.
So if i docker push image/haha:1.0.0
the second time i do this (provided that something changes) the first image gets untagged from AWS ECR
.
Is there a way to safely clean up all the registries from untagged images?
I actually forged a one line solution using
aws cli
What it's doing is:
tagStatus=UNTAGGED
batch-delete-image
You can delete all images in a single request, without loops:
First it gets a list of images that are untagged, in json format:
[ {"imageDigest": "sha256:..."}, {"imageDigest": "sha256:..."}, ... ]
Then it sends that list to
batch-image-delete
.The last
|| true
is required to avoid an error code when there are no untagged images.Now, that ECR support lifecycle policies (https://docs.aws.amazon.com/AmazonECR/latest/userguide/LifecyclePolicies.html) you can use it to delete the untagged images automatically.
From here: https://docs.aws.amazon.com/AmazonECR/latest/userguide/lpp_creation.html
Setting a Lifecycle policy is definitely the best way of managing this. That being said - if you do have a bunch of images that you want to delete keep in mind that the max for batch-delete-images is 100. So you need to do this is for the number of untagged images is greater than 100: