How to remove unused images in Azure with Scripted Actions

Learn how to clean up unused images in Azure by automating the process with Scripted Actions. Simplify your resource management and save costs with this step-by-step guide.
Picture of Andreas

Andreas

Table of contents

In Azure environments, unused resources, such as orphaned virtual machine disk images, can build up over time. These unused images not only clutter your resource inventory but also result in unnecessary storage costs. Manually identifying and removing these images is a tedious and inefficient process, particularly in large-scale or dynamic setups. This article introduces a practical and automated solution for identifying and cleaning up unused images in Azure using XOAP’s Scripted Action module. By following the outlined steps, you can simplify resource management, lower costs, and keep your Azure environment clean and well-organized.

Automating image cleanup in Azure with one script

💡 The script identifies unused images in Azure using the following logic:

Retrieve VMs: It fetches a list of all virtual machines (VMs) in the Azure subscription using the Get-AzVM -Status command. This ensures it has the most up-to-date information about the VMs and their status.

Compare image IDs: The script iterates through each VM and checks its StorageProfile.ImageReference.Id property, which indicates the image used by the VM. For each image in the specified resource group (retrieved with Get-AzImage -ResourceGroupName), it compares the image’s ID against the ImageReference.Id of the VMs.

Determine usage: If a match is found, the function Is-ImageInUse returns $true, indicating the image is currently in use by a VM. Moreover, if no match is found, the function returns $false, marking the image as unused.

Output and actions: For each image, the script logs its name and whether it is in use. If the image is not in use, additional actions (such as deletion) can be implemented following this check.

This approach makes sure that only images with no active VM dependencies are identified as unused, preventing accidental deletion of critical resources. Removing unused images from your Azure subscription is a simple process with XOAP. It involves just three easy steps:

  1. Add an Azure connection to your XOAP Workspace
  2. Create a Scripted Action with defined parameters
  3. Run the Scripted Action to remove unused images

Step 1: Add a Connection for your Azure Subscription

To begin, make sure your Azure subscription is connected to XOAP. If you haven’t set up a connection yet, navigate to the Connections section in XOAP. Click the green plus (+) button to create a new connection. Follow the on-screen instructions to securely link your Azure subscription. Once the connection is successfully established, you can proceed to the next steps.

In the “Add New Connection” slide-out panel, enter all the required connection details and click Save to complete the setup.

If you need additional guidance on adding a connection in XOAP, feel free to visit our documentation page.

Step 2: Create your scripted action

Next, navigate to Platform Management and select Scripted Actions. In the upper-right corner, click the green plus (+) button to create a new Scripted Action.

Give your Scripted Action a name and select the “Remove-UnusedImages.ps1” script from the resources. Optionally, you can add tags and a description to organize and document it. You can get the script from our GitHub.

In the second step, define your parameters. Some parameters are mandatory and must be provided, while others are optional. These optional parameters allow you to customize the script.

Explanation of Key Fields:

DryRun: It allows you to simulate the execution of a script or command without actually making any changes to the system.

ResourceGroup: Azure Resource Group that a particular command should target. If this field is left empty, the command will be applied to all resource groups.

Step 3: Complete the process

In the third step, select your connection and click Finish to complete the setup.

The final step is to run the Scripted Action you created. Locate your Scripted Action in the table, open the action menu, and select Run.

The Scripted Action will progress through the Queued and Running phases. Once completed, its status will change to Success and the unused images will be removed from Azure.

Safeguard critical resources

Using XOAP’s Scripted Actions module to delete unused Azure images is a great way to optimize costs and improve your resource management. Its flexibility to target specific resource groups or default to all secures a comprehensive cleanup. Key considerations include accurate detection of unused images, verifying dependencies, maintaining detailed logs, and adhering to RBAC policies.

Testing the script in a non-production environment and implementing a backup strategy further improve reliability. This approach boosts automation while safeguarding critical resources, making it a practical solution for Azure optimization.

Get the free script from our GitHub (don’t forget to follow us!) and test it out in XOAP for free. If you need help, fill out the form here and one of our experts will reach out. 

By subscribing, you accept the XOAP privacy policy and will receive product information from us.

Share post

More Posts

Scroll to Top