AZ Cleanup

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 will require the -SubscriptionID <guid>, and -ResourceGroup is optional. 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.

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>

So how do you delete them? First put the resources in a collection, then run the Remove-AZResource for example:

$Disks=Get-UnattachedDisks -SubscriptionID $SubscriptionID -ResourceGroup MyResourceGroup
foreach ($disk in $Disks) {Remove-AzResource -ResourceId $ -force}

Tagged , ,
%d bloggers like this: