Skip to content

Commit

Permalink
update delete sample (#22138)
Browse files Browse the repository at this point in the history
  • Loading branch information
annelo-msft authored Jun 24, 2021
1 parent b3224ee commit 8848724
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 13 deletions.
19 changes: 15 additions & 4 deletions sdk/containerregistry/Azure.Containers.ContainerRegistry/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ image.UpdateTagProperties("latest", new ArtifactTagProperties()
### Delete images

```C# Snippet:ContainerRegistry_Tests_Samples_DeleteImage
using Azure.Containers.ContainerRegistry;
using Azure.Identity;

// Get the service endpoint from the environment
Uri endpoint = new Uri(Environment.GetEnvironmentVariable("REGISTRY_ENDPOINT"));

Expand All @@ -177,13 +180,15 @@ foreach (string repositoryName in repositoryNames)
// Delete images older than the first three.
foreach (ArtifactManifestProperties imageManifest in imageManifests.Skip(3))
{
RegistryArtifact image = repository.GetArtifact(imageManifest.Digest);
Console.WriteLine($"Deleting image with digest {imageManifest.Digest}.");
Console.WriteLine($" This image has the following tags: ");
Console.WriteLine($" Deleting the following tags from the image: ");
foreach (var tagName in imageManifest.Tags)
{
Console.WriteLine($" {imageManifest.RepositoryName}:{tagName}");
image.DeleteTag(tagName);
}
repository.GetArtifact(imageManifest.Digest).Delete();
image.Delete();
}
}
```
Expand Down Expand Up @@ -251,6 +256,10 @@ await image.UpdateTagPropertiesAsync("latest", new ArtifactTagProperties()
### Delete images asynchronously

```C# Snippet:ContainerRegistry_Tests_Samples_DeleteImageAsync
using System.Linq;
using Azure.Containers.ContainerRegistry;
using Azure.Identity;

// Get the service endpoint from the environment
Uri endpoint = new Uri(Environment.GetEnvironmentVariable("REGISTRY_ENDPOINT"));

Expand All @@ -270,13 +279,15 @@ await foreach (string repositoryName in repositoryNames)
// Delete images older than the first three.
await foreach (ArtifactManifestProperties imageManifest in imageManifests.Skip(3))
{
RegistryArtifact image = repository.GetArtifact(imageManifest.Digest);
Console.WriteLine($"Deleting image with digest {imageManifest.Digest}.");
Console.WriteLine($" This image has the following tags: ");
Console.WriteLine($" Deleting the following tags from the image: ");
foreach (var tagName in imageManifest.Tags)
{
Console.WriteLine($" {imageManifest.RepositoryName}:{tagName}");
await image.DeleteTagAsync(tagName);
}
await repository.GetArtifact(imageManifest.Digest).DeleteAsync();
await image.DeleteAsync();
}
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
A common use case for Azure Container Registries is to scan the repositories in a registry and delete all but the most recent *n* images, or all images older than a certain date. This sample illustrates how to use the .NET ACR SDK to delete all but the latest three images.

```C# Snippet:ContainerRegistry_Tests_Samples_DeleteImage
using Azure.Containers.ContainerRegistry;
using Azure.Identity;

// Get the service endpoint from the environment
Uri endpoint = new Uri(Environment.GetEnvironmentVariable("REGISTRY_ENDPOINT"));

Expand All @@ -22,13 +25,15 @@ foreach (string repositoryName in repositoryNames)
// Delete images older than the first three.
foreach (ArtifactManifestProperties imageManifest in imageManifests.Skip(3))
{
RegistryArtifact image = repository.GetArtifact(imageManifest.Digest);
Console.WriteLine($"Deleting image with digest {imageManifest.Digest}.");
Console.WriteLine($" This image has the following tags: ");
Console.WriteLine($" Deleting the following tags from the image: ");
foreach (var tagName in imageManifest.Tags)
{
Console.WriteLine($" {imageManifest.RepositoryName}:{tagName}");
image.DeleteTag(tagName);
}
repository.GetArtifact(imageManifest.Digest).Delete();
image.Delete();
}
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@

A common use case for Azure Container Registries is to scan the repositories in a registry and delete all but the most recent *n* images, or all images older than a certain date. This sample illustrates how to use the .NET ACR SDK to delete all but the latest three images.

Please note:

- The `System.Linq.Async` package provides the LINQ `Skip` method. For more information on using this package with AsyncPageable, please see [Using System.Linq.Async with AsyncPageable](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/core/Azure.Core/samples/Response.md#using-systemlinqasync-with-asyncpageable).

- The operations in this sample are run in series, but could be parallelized using the new `Parallel.ForEachAsync` method in [.NET 6](https://dotnet.microsoft.com/download/dotnet/6.0).

```C# Snippet:ContainerRegistry_Tests_Samples_DeleteImageAsync
using System.Linq;
using Azure.Containers.ContainerRegistry;
using Azure.Identity;

// Get the service endpoint from the environment
Uri endpoint = new Uri(Environment.GetEnvironmentVariable("REGISTRY_ENDPOINT"));

Expand All @@ -22,13 +32,15 @@ await foreach (string repositoryName in repositoryNames)
// Delete images older than the first three.
await foreach (ArtifactManifestProperties imageManifest in imageManifests.Skip(3))
{
RegistryArtifact image = repository.GetArtifact(imageManifest.Digest);
Console.WriteLine($"Deleting image with digest {imageManifest.Digest}.");
Console.WriteLine($" This image has the following tags: ");
Console.WriteLine($" Deleting the following tags from the image: ");
foreach (var tagName in imageManifest.Tags)
{
Console.WriteLine($" {imageManifest.RepositoryName}:{tagName}");
await image.DeleteTagAsync(tagName);
}
await repository.GetArtifact(imageManifest.Digest).DeleteAsync();
await image.DeleteAsync();
}
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ public void DeleteImages()
Environment.SetEnvironmentVariable("REGISTRY_ENDPOINT", TestEnvironment.Endpoint);

#region Snippet:ContainerRegistry_Tests_Samples_DeleteImage
#if SNIPPET
using Azure.Containers.ContainerRegistry;
using Azure.Identity;
#endif

// Get the service endpoint from the environment
Uri endpoint = new Uri(Environment.GetEnvironmentVariable("REGISTRY_ENDPOINT"));

Expand All @@ -39,13 +44,15 @@ public void DeleteImages()
// Delete images older than the first three.
foreach (ArtifactManifestProperties imageManifest in imageManifests.Skip(3))
{
RegistryArtifact image = repository.GetArtifact(imageManifest.Digest);
Console.WriteLine($"Deleting image with digest {imageManifest.Digest}.");
Console.WriteLine($" This image has the following tags: ");
Console.WriteLine($" Deleting the following tags from the image: ");
foreach (var tagName in imageManifest.Tags)
{
Console.WriteLine($" {imageManifest.RepositoryName}:{tagName}");
image.DeleteTag(tagName);
}
repository.GetArtifact(imageManifest.Digest).Delete();
image.Delete();
}
}
#endregion
Expand All @@ -57,7 +64,13 @@ public async Task DeleteImagesAsync()
{
Environment.SetEnvironmentVariable("REGISTRY_ENDPOINT", TestEnvironment.Endpoint);

# region Snippet:ContainerRegistry_Tests_Samples_DeleteImageAsync
#region Snippet:ContainerRegistry_Tests_Samples_DeleteImageAsync
#if SNIPPET
using System.Linq;
using Azure.Containers.ContainerRegistry;
using Azure.Identity;
#endif

// Get the service endpoint from the environment
Uri endpoint = new Uri(Environment.GetEnvironmentVariable("REGISTRY_ENDPOINT"));

Expand All @@ -77,13 +90,15 @@ public async Task DeleteImagesAsync()
// Delete images older than the first three.
await foreach (ArtifactManifestProperties imageManifest in imageManifests.Skip(3))
{
RegistryArtifact image = repository.GetArtifact(imageManifest.Digest);
Console.WriteLine($"Deleting image with digest {imageManifest.Digest}.");
Console.WriteLine($" This image has the following tags: ");
Console.WriteLine($" Deleting the following tags from the image: ");
foreach (var tagName in imageManifest.Tags)
{
Console.WriteLine($" {imageManifest.RepositoryName}:{tagName}");
await image.DeleteTagAsync(tagName);
}
await repository.GetArtifact(imageManifest.Digest).DeleteAsync();
await image.DeleteAsync();
}
}

Expand Down

0 comments on commit 8848724

Please sign in to comment.