Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete aws ECR repository which contain images through cloudformation?

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

like image 828
mahendra rathod Avatar asked Mar 14 '18 12:03

mahendra rathod


2 Answers

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)

like image 155
josescuderoh Avatar answered Sep 30 '22 19:09

josescuderoh


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.

like image 23
Mark White Avatar answered Sep 30 '22 18:09

Mark White