Created
December 4, 2017 03:29
-
-
Save n1te1337/232436be7c6082c54b90f6cee99ff790 to your computer and use it in GitHub Desktop.
Simple python 3 script to delete ECR images older than X days
This file contains 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
import subprocess | |
import json | |
from datetime import datetime | |
dry_run = False | |
region = 'us-east-1' | |
delete_older_than = 60 | |
repository = 'repository_name' | |
ecr_get_cmd = 'aws --region {} ecr describe-images --repository-name "{}"'.format(region, repository) | |
ecr_delete_cmd = 'aws --region {} ecr batch-delete-image --repository-name "{}" --image-ids '.format(region, repository) | |
ecr_get_cmd_result = subprocess.check_output(ecr_get_cmd, stderr=subprocess.STDOUT, shell=True) | |
images = json.loads(ecr_get_cmd_result)['imageDetails'] | |
print('TOTAL IMAGES: {}'.format(len(images))) | |
images_to_be_deleted = [] | |
for image in images: | |
days_old = (datetime.now() - datetime.fromtimestamp(int(image['imagePushedAt']))).days | |
if days_old > delete_older_than: | |
images_to_be_deleted.append(image) | |
print('IMAGES TO BE DELETED: {}'.format(len(images_to_be_deleted))) | |
batch_delete_size = 100 | |
for i in range(0, len(images_to_be_deleted), batch_delete_size): | |
ids_delete_arg = ' '.join(['imageDigest=' + image['imageDigest'] for image in images_to_be_deleted[i:i+batch_delete_size]]) | |
if dry_run: | |
print(ecr_delete_cmd + ids_delete_arg) | |
else: | |
print(subprocess.check_output(ecr_delete_cmd + ids_delete_arg, stderr=subprocess.STDOUT, shell=True)) | |
print('DONE') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment