W
wernerrall
Automating Disk Cleanup in Azure Using PowerShell, Azure Resource Graph and LastOwnershipUpdateTime
Managing resources in Azure efficiently is crucial for cost optimization and operational efficiency. Recently, a new property,
LastOwnershipUpdateTime
, was introduced for Azure disks, which helps track the last state change of a disk. In this blog, we'll explore how to automate the cleanup of unattached disks older than 60 days using PowerShell and Azure Resource Graph queries.
Introduction
In this post, we'll cover:
- How to query Azure resources to find unattached disks using
LastOwnershipUpdateTime
. - How to automate the deletion of these disks using PowerShell.
Setting Up
Before we dive into the script, ensure you have:
- The latest version of the Azure PowerShell module (
Az
module version 11.0.0 or newer). - Appropriate permissions to manage Azure resources.
Step-by-Step Guide
1. Ensure You Have the Latest Azure PowerShell Module
First, you need to update your Azure PowerShell modules to the latest version to access the new properties.
# Check for existing Az modules
get-module -ListAvailable -Name Az* | Select-Object Name, Version
# Uninstall all old versions of Az modules
Get-Module -ListAvailable Az* | foreach { Uninstall-Module -Name $_.Name -RequiredVersion $_.Version }
# Install the latest Az module
Install-Module -Name Az -AllowClobber -Scope CurrentUser
# Verify the installation
Get-Module -ListAvailable -Name Az* | Select-Object Name, Version
2. Writing the Azure Resource Graph Query
The following query retrieves disks that haven't had ownership updates in the last 60 days:
Code:
$disksToBeRemoved = Search-AzGraph -Query '
resources
| where type == "microsoft.compute/disks"
| where todatetime(properties.LastOwnershipUpdateTime) < ago(60d)
| project name, diskState = properties.diskState, lastUpdateTime = format_datetime(todatetime(properties.LastOwnershipUpdateTime), "dd-MM-yyyy")
'
3. Automating the Disk Deletion
With the disks identified, we can automate their deletion using PowerShell:
Code:
foreach ($disk in $disksToBeRemoved) {
# Simulate the deletion action
Write-Output "Disk: $($disk.name), Last Update: $($disk.lastUpdateTime)"
# Actual deletion command
Remove-AzDisk -Name $disk.name
}
4. Putting it all together
Let's take a look at all of the pieces combined:
Code:
# Ensure you have the latest Azure PowerShell module
Install-Module -Name Az -AllowClobber -Scope CurrentUser -Force
# Authenticate to Azure
Connect-AzAccount
# Define the query to find disks that haven't had ownership updates in the last 60 days
$disksToBeRemoved = Search-AzGraph -Query '
resources
| where type == "microsoft.compute/disks"
| where todatetime(properties.LastOwnershipUpdateTime) < ago(60d)
| project name, diskState = properties.diskState, lastUpdateTime = format_datetime(todatetime(properties.LastOwnershipUpdateTime), "dd-MM-yyyy")
'
# Loop through each disk and delete it
foreach ($disk in $disksToBeRemoved) {
# Output the disk information for verification
Write-Output "Disk: $($disk.name), Last Update: $($disk.lastUpdateTime)"
# Actual deletion command
Remove-AzDisk -Name $disk.name -Force
}
Explanation
- Install-Module: Ensures you have the latest Azure PowerShell module installed.
- Connect-AzAccount: Authenticates your session with Azure.
- Search-AzGraph: Queries Azure Resource Graph to find disks older than 60 days.
- Remove-AzDisk: Deletes each disk found by the query.
Conclusion
Automating the cleanup of unattached disks older than 60 days helps optimize resource usage and reduce costs. By following this guide, you can implement a similar solution in your Azure environment. If you have any questions or feedback, feel free to leave a comment below.
Disclaimer
The sample scripts are not supported under any Microsoft standard support program or service. The sample scripts or Power BI Dashboards are provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the sample scripts and documentation remains with you. In no event shall Microsoft, its authors, or anyone else involved in the creation, production, or delivery of the scripts or Power BI Dashboards be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample scripts or documentation, even if Microsoft has been advised of the possibility of such damages. This blog post was written with the help of generative AI.
Continue reading...