I want to delete all ecr images which are untagged
import boto3
import pprint
s3 = boto3.resource('s3')
for bucket in s3.buckets.all():
print(bucket.name)
pp = pprint.PrettyPrinter(indent=4)
client = boto3.client('ecr', region_name='us-west-2')
response = client.describe_repositories(repositoryNames=['localstack-centos'])
#print(response)
""" response1 = client.describe_images(
repositoryName='localstack-centos',
#maxResults=2,
imageIds=[
{
'imageTag': 'untagged'
},
],
) """
#print(response1)
response2 = client.list_images(
repositoryName='localstack-centos',
maxResults=123,
filter={
'tagStatus': 'UNTAGGED'
}
)
print(response2)
pp.pprint(response2)
response = client.batch_delete_image(
registryId='string',
repositoryName='localstack-centos',
imageIds=[
{
'imageTag': 'untagged'
},
]
)
I am able to list all ecr images but not able to delete "untagged" images if I replace 'untagged' with latest , that image gets deleted how would I refer all untagged images
You can use imageDigest
to delete an image. Collect the imageDigest
of the images that you want to delete first, and delete it.
import boto3
client = boto3.client('ecr')
response = client.list_images(repositoryName='localstack-centos')
untaggedImageList = [image for image in response['imageIds'] if image['imageTag'] == 'untagged']
response2 = client.batch_delete_image(repositoryName='localstack-centos', imageIds=untaggedImageList)
print(response2)