How to delete image from Azure Container Registry
Asked Answered
H

9

16

Is there a way to delete specific tags only? I only found a way to delete the whole registry using the REST/cli-acr

Thanks

Hydropic answered 3/1, 2017 at 15:33 Comment(1)
It looks like there is no way to do this right now. The delete function will only be implemented in a future release.Hydropic
H
9

UPDATE COPIED FROM BELOW:

As an update, today we've released a preview of several features including repository delete, Individual Azure Active Directory Logins and Webhooks.

Original answer:

We are hardening up the registry for our GA release later this month. We've deferred all new features while we focus on performance, reliability and additional azure data centers, delivering ACR across all public data centers by GA. We will provide deleting of images and tags in a future release. We're started to use https://github.com/Azure/acr/ to track features and bugs. Delete is captured here: https://github.com/Azure/acr/issues/33

Thanks for the feedback, Steve

Hofuf answered 18/3, 2017 at 19:42 Comment(0)
U
9

You can use Azure CLI 2.0 to delete images from a repository with a given tag:

az acr repository delete -n MyRegistry --repository MyRepository --tag MyTag

  • MyRegistry is the name of your Azure Container Registry
  • MyRepository is the name of the repository
  • MyTag denotes the tag you want to delete.

You can also choose to delete the whole repository by omitting --tag MyTag. More information about the az acr repository delete command can be found here: https://learn.microsoft.com/en-us/cli/azure/acr/repository#delete

Uralite answered 17/8, 2017 at 13:48 Comment(0)
O
9

Here is a powershell script that deletes all Azure Container Registry tags except for tags MyTag1 and MyTag2:

az acr repository show-tags -n MyRegistry --repository MyRepository | ConvertFrom-String | %{$_.P2 -replace "[`",]",""} | where {$_ -notin "MyTag1","MyTag2"  } | % {az acr repository delete -n MyRegistry --repository MyRepository --tag $_ --yes}

It uses Azure CLI 2.0.

Olgaolguin answered 7/2, 2018 at 9:37 Comment(2)
--tag did not work for me. I changed your script az acr repository show-tags -n MyRegistry --repository MyRepository | ConvertFrom-String | %{$_.P2 -replace "[",]",""} | where {$_ -notin "skipthis","andthis" } | % {az acr repository delete --name MyRegistry --image MyRepository:$_ --yes}`Fu
az acr repository show-tags -n MyRegistry --repository MyRepository -o tsv | where {$_ -notin "skipthis","andthis" } | % {az acr repository delete --name MyRegistry --image MyRepository:$_ --yes} using -o tsv so that string replace not requiredSuki
P
5

I had a similar problem where I wanted to remove historical images from the repository as our quota had reached 100%

I was able to do this by using the following commands in the Azure CLI 2.0. The process does the following : obtain a list of tags, filter it with grep and clean it up with sed before passing it to the delete command.

Get all the tags for the given repository

az acr repository show-tags -n [registry] --repository [repository] 

Get all the tags that start with the specific input and pipe that to sed which will remove the trailing comma

grep \"[starts with] | sed 's/,*$//g'

Using xargs, assign the output to the variable X and use that as the tag.

--manifest : Delete the manifest referenced by a tag. This also deletes any associated layer data and all other tags referencing the manifest.

--yes -y : Do not prompt for confirmation.

xargs -I X az acr repository delete -n [registry] --repository [repository] --tag X --manifest --yes

e.g. registry = myRegistry, repository = myRepo, I want to remove all tags that start with the tagname 'test' ( this would include test123, testing etc )

az acr repository show-tags -n myRegistry --repository myRepo | grep \"test | sed 's/,*$//g' | xargs -I X az acr repository delete -n myRegistry --repository myRepo --tag X --manifest --yes

More information can be found here Microsoft Azure Docs

Prosthesis answered 24/10, 2017 at 22:15 Comment(0)
S
4

Following answer from @christianliebel Azure CLI generates error unrecognized arguments: --tag MyTag:

➜ az acr repository delete -n MyRegistry --repository MyRepository --tag MyTag
az: error: unrecognized arguments: --tag MyTag

I was using:

➜ az --version
azure-cli                         2.11.1

This works:

➜ az acr repository delete --name MyRegistry --image Myrepository:Mytag
This operation will delete the manifest 'sha256:c88ac1f98fce390f5ae6c56b1d749721d9a51a5eb4396fbc25f11561817ed1b8' and all the following images: 'Myrepository:Mytag'.
Are you sure you want to continue? (y/n): y
➜

Microsoft Azure CLI docs example:

https://learn.microsoft.com/en-us/cli/azure/acr/repository?view=azure-cli-latest#az-acr-repository-delete-examples

Servais answered 10/9, 2020 at 13:2 Comment(0)
H
1

As an update, today we've released a preview of several features including repository delete, Individual Azure Active Directory Logins and Webhooks. Steve

Hofuf answered 6/7, 2017 at 20:0 Comment(0)
M
1

Following command helps while deleting specific images following a name or search pattern :-

az acr repository show-manifests -n myRegistryName --repository myRepositoryName --query '[].tags[0]' -o yaml | grep 'mySearchPattern' | sed 's/- /az acr repository delete --name myRegistryName --yes --image myRepositoryName:/g'

My use case was to delete all Container Registeries which were created before August in 2020 so I copied the output of the following command and then executed them, as my manifest names had creation Date like DDMMYYYY-HHMM:-

az acr repository show-manifests -n myRegistryName --repository myRepositoryName --query '[].tags[0]' -o yaml | grep '[0-7]2020-' | sed 's/- /az acr repository delete --name myRegistryName --yes --image myRepositoryName:/g'

Reference: Microsoft ACR CLI

Montherlant answered 11/8, 2020 at 12:10 Comment(0)
C
1

I have used the REST Api to delete the empty tagged images from a particular repository, documentation available here

import os
import sys
import yaml
import json
import requests

config = yaml.safe_load(
    open(os.path.join(sys.path[0], "acr-config.yml"), 'r'))
"""
Sample yaml file
acr_url: "https://youregistryname.azurecr.io"
acr_user_name: "acr_user_name_from_portal"
acr_password: "acr_password_from_azure_portal"
# Remove the repo name so that it will clean all the repos
repo_to_cleanup: some_repo
"""
acr_url = config.get('acr_url')
acr_user_name = config.get("acr_user_name")
acr_password = config.get("acr_password")
repo_to_cleanup = config.get("repo_to_cleanup")


def iterate_images(repo1, manifests):
    for manifest in manifests:
        try:
            tag = manifest['tags'][0] if 'tags' in manifest.keys() else ''
            digest = manifest['digest']
            if tag is None or tag == '':
                delete = requests.delete(f"{acr_url}/v2/{repo1}/manifests/{digest}", auth=(acr_user_name, acr_password))
                print(f"deleted the Tag = {tag} , Digest= {digest}, Status {str(delete)} from Repo {repo1}")
        except Exception as ex:
            print(ex)


if __name__ == '__main__':
    result = requests.get(f"{acr_url}/acr/v1/_catalog", auth=(acr_user_name, acr_password))
    repositories = json.loads(result.content)
    for repo in repositories['repositories']:
        if repo_to_cleanup is None or repo == repo_to_cleanup:
            manifests_binary = requests.get(f"{acr_url}/acr/v1/{repo}/_manifests", auth=(acr_user_name, acr_password))
            manifests_json = json.loads(manifests_binary.content)
            iterate_images(repo, manifests_json['manifests'])
Calculous answered 15/10, 2021 at 11:17 Comment(0)
F
0

I tried all commands but none worked.I though that it could be stacked so I went to my portal azure and deleted my repository by myself. It works

Florance answered 18/9, 2019 at 16:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.