Search code examples
azureazure-cli

How do you delete old vm versions using az cli?


I have some vm image versions in a vm image definition in a compute gallery. Azure cli's image commands will only work on the entire Azure marketplace (https://learn.microsoft.com/en-us/cli/azure/vm/image?view=azure-cli-latest#az-vm-image-show) and acr purge only works on Container Registries and won't accept the compute gallery name.

PURGE_CMD="acr purge \
>   --filter 'mydefinition:.*' \
>   --ago 7d --dry-run"

az acr run   --cmd "$PURGE_CMD"   --registry mycomputegallery  /dev/null

The resource with name 'mycomputegallery' and type 'Microsoft.ContainerRegistry/registries' could not be found

az gallery is also not working

az gallery service-artifact list --gallery-name mygallery --resource-group myrg
The command requires the extension gallery-service-artifact. Do you want to install it now? The command will continue to run after the extension is installed. (Y/n): Y
Run 'az config set extension.use_dynamic_install=yes_without_prompt' to allow installing extensions without prompt.
No matching extensions for 'gallery-service-artifact'. Use --debug for more information.

Solution

  • How do you delete old vm versions using az cli?

    Here is the Azure cli script to delete the VM versions from Azure compute gallery

    The below azure cli will delete the VM versions if date matches to specified date.

    Azure CLI

    $imagever = az sig image-version list --gallery-image-definition "Venkat1" --gallery-name "Venkatgallery" --resource-group "v-vallepu-mindtree" | ConvertFrom-Json
    
    $targetDate = "2024-06-05"
    
    foreach ($version in $imagever) {
        $publishedDate = [datetime]::Parse($version.publishingProfile.publishedDate)
        $formattedDate = $publishedDate.ToString("yyyy-MM-dd")
        
        if ($formattedDate -eq $targetDate) {
            $versionName = $version.name
            Write-Output "Deleting the Version: $versionName"
            
            az sig image-version delete --resource-group "v-vallepu-mindtree" --gallery-name "Venkatgallery" --gallery-image-definition "Venkat1" --gallery-image-version $versionName
        }
    }
    

    Output:

    enter image description here

    Here is the Azure Powershell script to delete the VM versions with date.

    Powershell

        $galleryversions = Get-AzGalleryImageVersion -ResourceGroupName v -GalleryName Venkatgallery -GalleryImageDefinitionName "Venkat1"
        
        foreach ($version in $galleryversions) {
            $publishedDate = [datetime]::Parse($version.PublishingProfile.PublishedDate)
            $date = $publishedDate.ToString("M/d/yyyy")
            
            if ($date -eq "6/5/2024") {
                $imgversionname = $version.Name
                Write-Output : "Deleting the Version: $imgversionname"
                Remove-AzGalleryImageVersion -ResourceGroupName "v" -GalleryName "Venkatgallery" -GalleryImageDefinitionName "Venkat1" -Name $version.Name
            }
        }
    

    Output

    enter image description here

    Reference: az sig image-version delete