I already published a script for cleaning up Azure resources, but found that with the introduction of the new AZ commands and later versions of the PShell CMDLets it was getting out of date fast.
So I worked on a new module (.psm1) to replace it and give me more freedom to use the unused objects in Azure in the command line.
For example, you might want to tag unused resources, rather than deleting them, or lock them from deletion..
Hence the new PShell module AZCleanup.psm1 which you can use to get unused resources and then clean them out.. or lock them or whatever you want.
Import the module in your PowerShell window using
Import-Module AZCleanup.psm1
Then you will have new commandlets available, amongst of which:
- Get-UnattachedNICs
- Get-UnattachedPIPs
- Get-UnattachedDisks
All of these can use the -SubscriptionID <guid>, and -ResourceGroup options. In short, you can now easily get all the unused disks in a subscription (or resourcegroup in a subscription) and do something with the result.
For example it is possible to delete all the unattached disks in a resource group quickly by issuing:
Get-UnattachedDisks -ResourceGroup MyRG | Remove-AzDisk -Force -AsJob
This will create individual Remove-AzDisk jobs for all the unattached disks found in the MyRG resource group.
Other functions are also available in the script to lock/unlock resource, but this is still under review and up for further refinement.. you can try them by typing: SelectOperationsTarget -SubscriptionID <SubscriptionGUID>