How to delete untagged images from AWS ECR Container Registry

前端 未结 4 1459
礼貌的吻别
礼貌的吻别 2020-12-16 12:34

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

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-16 13:24

    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.

提交回复
热议问题