Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete untagged images from AWS ECR Container Registry

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?

like image 302
Andrea Baccega Avatar asked Dec 03 '16 15:12

Andrea Baccega


People also ask

How can I untag photos from ECR?

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.

How do I delete ECR repository AWS?

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.


1 Answers

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.

like image 188
nfvs Avatar answered Sep 20 '22 17:09

nfvs