Created
June 12, 2023 11:33
-
-
Save jimbo8098/59f8016da40bfa41be84ad4ee9893987 to your computer and use it in GitHub Desktop.
Clear AWS Images
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
# Delete ECR Docker image in batch | |
# Supports multiple regions if necessary | |
$repositories = @( | |
"image:tag" | |
#... | |
); | |
# Checked to verify which tags are present in a given region | |
$primaryRegion = "eu-west-2" | |
# Regions the image should be deleted from | |
$deleteRegions = @("eu-west-1","eu-west-2","eu-central-1") | |
$repositories | % { | |
$repoName,$tag = $_.Split(":") | |
$resArr = $(aws ecr describe-images --region $primaryRegion --repository-name "${repoName}" --query "imageDetails[].{repositoryName:repositoryName,imageDigest:imageDigest,imageTags:imageTags}" | ConvertFrom-Json) | |
if($resArr.Count -eq 1 ) { | |
$response = $resArr[0] | |
} else { | |
return | |
} | |
if($response.imageTags.Where{ $_ -eq $tag } -gt 1) { | |
echo "${repoName}:${tag} found - deleting..." | |
$deleteRegions | % { | |
echo "$_" | |
aws ecr batch-delete-image --repository-name $repoName --image-ids "imageDigest=$($response.imageDigest)" --region $_ | |
} | |
echo "Done" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment