How to delete aws ECR repository which contain images through cloudformation? getting below error while deleting it.
The repository with name 'test' in registry with id '**********' cannot be deleted because it still contains images
Leaving my approach to solve this using Python's boto3 client. (1) Empty repository and then (2) delete stack.
import boto3
ecr_client = boto3.client('ecr')
...
# Apply only to ecr cfn template
if '-ecr' in stack_name:
    print('Deleting existing images...')
    image_ids = ecr_client.list_images(repositoryName=ECR_REPO_NAME)['imageIds']
    ecr_client.batch_delete_image(
        repositoryName=ECR_REPO_NAME,
        imageIds=image_ids
    )
    print('ECR repository is now empty.')
# Now delete stack containing ECR repository
delete_stack(**cf_template)
I was able to do this by first deleting all images in ECR and then going back to CloudFormation and deleting again. Instructions for deleting images are here: https://docs.aws.amazon.com/AmazonECR/latest/userguide/delete_image.html. After I did that, I was able to head back to CloudFormation and delete with no problems.
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