Skip to main content

ECR: Delete Unused Tagged Images

Difficulty: Medium

Description

These repositories hold tagged images nobody has pulled in 90 days, and you keep paying to store them. The most recently pushed tagged image of each repository is never recommended for deletion. Rows marked Used by a Running Workload still contain images that a running ECS task or a Lambda function references: they are listed because they are old, but review them before deleting them. Unused Since tells you when the most recently used of these images was last pulled, and Sample Tags names a few of them so you can recognize the release line. For a CDK assets repository (cdk-hnb659fds-container-assets-…), use cdk gc, which only deletes the images no deployed stack references.

Selection Criteria

  • The image is tagged and active. Untagged images are covered by ECR: Add a Lifecycle Policy for Untagged Images, and archived images are excluded.

  • The image has not been pulled in the last 90 days. An image that was never pulled counts from its push date, so it must have been pushed more than 90 days ago.

  • The image is not the most recently pushed tagged image of its repository: that image is never recommended, since it is usually the current release.

  • An image that a running ECS task or a Lambda function (published versions included) references is still listed, and its row is flagged Used by a Running Workload with the number and size of those images.

  • The repository has a billed ECR storage cost over the last 31 days.

  • Images a lifecycle policy expires stay listed, but are not counted in the realized saving.

Expected Saving

The saving is the share of the repository's billed storage cost held by its unused tagged images. We take the repository's real storage cost from the CUR over the last 31 days (net of discounts), annualize it, and multiply it by the share of image bytes held by those images. Images still in use are included, since they are part of what the row lists. Data transfer is excluded from the saving because deleting images does not reduce it.

Example: a repository billing $500/year of storage, where unused tagged images hold 40% of the image bytes, shows a potential saving of 500 × 0.40 = $200/year.

The realized saving is the share of the repository's storage cost freed by the deleted images. A deletion counts once the nightly inventory no longer finds the image. An image pulled again does not count, and neither does an image a lifecycle policy expired, since the policy removed it, not this recommendation. Deleting the whole repository resolves the recommendation.

ECR bills each image layer once per repository, and images often share layers. Deleting an image can therefore free less storage than its size, and the bill can drop less than the saving shown.

How to apply

In the ECR console, open the repository, sort the images by Last recorded pull time, select the old tagged images you no longer deploy, and choose Delete. With the AWS CLI, list the images with their tags, push and pull dates, then delete the ones you selected by digest:

aws ecr describe-images --repository-name my-repo --query 'imageDetails[].[imageDigest,imageTags,imagePushedAt,lastRecordedPullTime]' --output table
aws ecr batch-delete-image --repository-name my-repo --image-ids imageDigest=sha256:...

To keep the repository clean afterwards, add a lifecycle policy that expires tagged images beyond the number of releases you keep.

Operational Impact

  • ✅ No downtime for unused images: nothing has pulled them in 90 days, and the most recently pushed tagged image is never recommended.

  • ⏳ Credited at the next sync: the realized saving appears once the next nightly inventory no longer finds the deleted images.

  • ⚠️ Permanent deletion: a deleted image cannot be restored, so a rollback to one of its tags will fail. Keep the tags you may still deploy.

  • ⚠️ Images still in use: on rows flagged Used by a Running Workload, deleting the referenced images can stop an ECS service from scaling or redeploying, or a Lambda version from starting. Check the task definitions and function versions first.

  • ⚠️ CDK assets repositories: one repository holds the images of every stack in the account and region, so only its single newest image is skipped. A failed update rolls back to the current image, which is in use and flagged. Prefer cdk gc, which checks the deployed templates.

  • ⚠️ Pull time is approximate: ECR refreshes the last pull time at least once every 24 hours, so an image pulled in the last day may still look unused.

References

Did this answer your question?