Created
August 27, 2022 00:27
-
-
Save guitarrapc/48147b68ebe9932a82caab539bce222b to your computer and use it in GitHub Desktop.
Force delete your images object from ecr repository
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
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>net6.0</TargetFramework> | |
<ImplicitUsings>enable</ImplicitUsings> | |
<Nullable>enable</Nullable> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="AWSSDK.ECR" Version="3.7.4.58" /> | |
<PackageReference Include="AWSSDK.SecurityToken" Version="3.7.1.189" /> | |
</ItemGroup> | |
</Project> |
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
using Amazon.ECR.Model; | |
async Task Main() | |
{ | |
var repositories = new[] | |
{ | |
"REPOSITORY_NAME", | |
}; | |
foreach (var repository in repositories) | |
{ | |
Console.WriteLine($"Delete all image of ecr: {repository}"); | |
var client = new Amazon.ECR.AmazonECRClient(); | |
ListImagesResponse? response = null; | |
var count = 0; | |
do | |
{ | |
response = await client.ListImagesAsync(new ListImagesRequest | |
{ | |
RepositoryName = repository | |
}); | |
if (response.ImageIds.Count > 0) | |
{ | |
count += response.ImageIds.Count; | |
if (count % 10000 == 0) Console.WriteLine($"{DateTime.Now}: Deleted {count} objects."); | |
await client.BatchDeleteImageAsync(new BatchDeleteImageRequest | |
{ | |
RepositoryName = repository, | |
ImageIds = response.ImageIds, | |
}); | |
} | |
} while (!string.IsNullOrEmpty(response.NextToken)); | |
Console.WriteLine($"Completed repository image deletion. {repository}"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment