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.
In this post, we'll cover:
LastOwnershipUpdateTime
.Before we dive into the script, ensure you have:
Az
module version 11.0.0 or newer).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:
$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:
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:
# 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
}
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.