How to clean-up obsolete Image Versions in Azure Shared Image Gallery

The case

In my recent try-outs with Windows Server 2022 on Azure, I’ve created many different Shared Image Gallery Image Versions via Azure Image Builder. A lot of trial and error eventually turns out in a massive list of different image versions.

We don’t need all of these image versions in many test and production cases, of course. Looking back to Citrix Provisioning Services, you eventually would merge the last set of image versions you created into a new baseline.

The solution

There isn’t a real merge option within Shared Image Gallery. However, we can, of course, delete a couple of these Image Versions and keep the ones we want to use as a baseline.
We can delete these image versions via the Azure Portal. However, it’s a lot of clicking around. So I decided to write a small script that can automate this for you.

Below you can find an overview of the image versions we currently have in place as an example.

Now I only want to keep version 1.0.0 as this was the first image I’ve uploaded from a managed image and the latest version.

The Script

By running the following script, which you can find on my GitHub repo, you’re prompted to select your SIG, Image Definition, and the versions you want to delete within that Image Definition.

#NOTE: This script can be used to cleanup obsolete image versions in your shared image gallery

#region shared image gallery details

$imagegallery = Get-AzGallery | Out-GridView -Title "Select the SIG you want to use" -PassThru
$imagegallerydefinitioninfo = Get-AzGalleryImageDefinition -GalleryName $imagegallery.Name -ResourceGroupName $imagegallery.ResourceGroupName | Out-GridView -Title "Select the SIG Definition" -PassThru
$imagegalleryinfo = Get-AzGalleryImageVersion -GalleryName $imagegallery.Name -ResourceGroupName $imagegallery.ResourceGroupName -GalleryImageDefinitionName $imagegallerydefinitioninfo.Name | Out-GridView -Title "Select the SIG Image Versions you want to delete" -OutputMode Multiple


#region delete image versions

foreach ($imageversion in $imagegalleryinfo)
Write-Host "Removing $($ from $($imagegallerydefinitioninfo.Name)" -ForegroundColor Red
    Remove-AzGalleryImageVersion -GalleryName $imagegallery.Name -GalleryImageDefinitionName $imagegallerydefinitioninfo.Name -Name $imageversion.Name -ResourceGroupName $imageversion.ResourceGroupName -force -AsJob


After running the script, you can go back to the Azure Portal and you’ll see the image versions being deleted.

We run the remove operation in “force” mode and “AsJob” to speed up the process, be careful when selecting your image versions in the Out-Gridview. If you want to built-in some additional safety, remove the “-force” parameter from the script.

Thank you!

Thank you for reading through this blog post. I hope I have been able to assist you in cleaning up your Shared Image Gallery Image versions.

If you encounter any new insights, feel free to drop me a comment or contact me via mail or other social media channels

Leave a Reply

Your email address will not be published. Required fields are marked *

Please reload

Please Wait