Last active
January 7, 2025 10:00
-
-
Save filipeandre/22b2a7bf22864f4ab2284313c759ad34 to your computer and use it in GitHub Desktop.
For all ECR repositories, delete all images except the 2 newest.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| python3 - <<EOF | |
| import boto3 | |
| ecr = boto3.client('ecr') | |
| repo_prefix = "dev-" | |
| # Paginate through repositories | |
| paginator = ecr.get_paginator('describe_repositories') | |
| for page in paginator.paginate(): | |
| for repo in page['repositories']: | |
| repo_name = repo['repositoryName'] | |
| if repo_name.startswith(repo_prefix): | |
| print(f"Processing repository: {repo_name}") | |
| # Paginate through images in the repository | |
| image_paginator = ecr.get_paginator('list_images') | |
| for image_page in image_paginator.paginate(repositoryName=repo_name): | |
| image_ids = image_page.get('imageIds', []) | |
| if image_ids: | |
| print(f"Deleting {len(image_ids)} images from repository: {repo_name}") | |
| ecr.batch_delete_image(repositoryName=repo_name, imageIds=image_ids) | |
| # Delete the repository | |
| print(f"Deleting repository: {repo_name}") | |
| ecr.delete_repository(repositoryName=repo_name, force=True) | |
| EOF |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| python3 - <<EOF | |
| import boto3 | |
| ecr = boto3.client('ecr') | |
| # Paginate through all repositories | |
| paginator = ecr.get_paginator('describe_repositories') | |
| for page in paginator.paginate(): | |
| for repo in page['repositories']: | |
| repo_name = repo['repositoryName'] | |
| print(f"Processing repository: {repo_name}") | |
| # Fetch all image IDs across all pages | |
| image_paginator = ecr.get_paginator('list_images') | |
| all_images = [] | |
| for image_page in image_paginator.paginate(repositoryName=repo_name): | |
| all_images.extend(image_page.get('imageIds', [])) | |
| # Fetch details of all images | |
| if all_images: | |
| image_details = [] | |
| for i in range(0, len(all_images), 100): # Handle batch limits (100 IDs per request) | |
| batch = all_images[i:i + 100] | |
| image_details.extend( | |
| ecr.describe_images(repositoryName=repo_name, imageIds=batch).get('imageDetails', []) | |
| ) | |
| # Sort all images by 'imagePushedAt' (newest first) | |
| image_details.sort(key=lambda img: img.get('imagePushedAt', 0), reverse=True) | |
| # Retain the 2 most recent images and delete the rest | |
| images_to_delete = image_details[2:] | |
| if images_to_delete: | |
| print(f"Deleting {len(images_to_delete)} older images from repository: {repo_name}") | |
| for i in range(0, len(images_to_delete), 100): # Handle batch limits for delete | |
| batch_to_delete = images_to_delete[i:i + 100] | |
| ecr.batch_delete_image( | |
| repositoryName=repo_name, | |
| imageIds=[{'imageDigest': img['imageDigest']} for img in batch_to_delete] | |
| ) | |
| else: | |
| print(f"No images found in repository: {repo_name}") | |
| EOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment