从 Azure Container Registry 删除大仓库
Azure Container Registry (ACR) 是 Azure 提供的私有 Docker 镜像仓库服务。随着时间的推移,仓库中可能会积累大量的镜像和标签,导致仓库变得非常大。对于大仓库的删除操作,直接使用常规的删除命令可能会失败,尤其是在仓库中包含大量镜像和标签时。
比如,在 Azure Portal 上尝试删除一个大仓库时,可能会遇到如下错误:
{"type":"MsPortalFx.Errors.AjaxError","baseTypes":["MsPortalFx.Errors.AjaxError","MsPortalFx.Errors.Error"],"data":{"uri":"https://<acr-name>.azurecr.io/acr/v1/repoName","type":"DELETE","pathAndQuery":"","failureCause":"","sessionId":"198926f051a4401bb8b1d1fa4b168918","status":0,"statusText":"error","duration":120427.89999997616},"extension":"Microsoft_Azure_ContainerRegistries","errorLevel":0,"timestamp":4780529.699999988,"name":"AjaxError","innerErrors":[],"textStatus":"error","errorThrown":"","jqXHR":{"readyState":0,"status":0,"statusText":"error"}}
而使用 Azure CLI 删除大仓库:
az acr repository delete --name <acr-name> --repository <repository-name>
也可能遇到问题:
Could not delete the requested data.
此时可以使用 az acr run 命令结合 acr purge 先清理仓库中的镜像标签,再删除仓库。以下是一个示例命令(注意此命令覆盖了默认超时时间),并使用正则表达式匹配仓库中的标签:
az acr run --registry <acr-name> --timeout 10800 --cmd 'acr purge --filter "<repository-name>:.*" --ago 0d --untagged' /dev/null --subscription '<subscription id or name>'
其中,--ago 0d 表示清理 0 天前的标签(即尽可能全量清理);如需保留最近一段时间的镜像,可将该参数改为例如 --ago 7d。
清理完成后,再执行仓库删除命令:
az acr repository delete --name <acr-name> --repository <repository-name>