Skip to content

Instantly share code, notes, and snippets.

@guitarrapc
Created August 27, 2022 00:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guitarrapc/48147b68ebe9932a82caab539bce222b to your computer and use it in GitHub Desktop.
Save guitarrapc/48147b68ebe9932a82caab539bce222b to your computer and use it in GitHub Desktop.
Force delete your images object from ecr repository
<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>
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