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?
From the navigation bar, choose the Region that contains the image to delete. In the navigation pane, choose Repositories. On the Repositories page, choose the repository that contains the image to delete. On the Repositories: repository_name page, select the box to the left of the image to delete and choose Delete.
Open the Amazon ECR console at https://console.aws.amazon.com/ecr/repositories . From the navigation bar, choose the Region that contains the repository to delete. In the navigation pane, choose Repositories. On the Repositories page, choose the Private tab and then select the repository to delete and choose Delete.
You can delete all images in a single request, without loops:
IMAGES_TO_DELETE=$( aws ecr list-images --region $ECR_REGION --repository-name $ECR_REPO --filter "tagStatus=UNTAGGED" --query 'imageIds[*]' --output json ) aws ecr batch-delete-image --region $ECR_REGION --repository-name $ECR_REPO --image-ids "$IMAGES_TO_DELETE" || true
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With