TIP: Cleaning up associated Azure resources after DSVM instance deletion RRS feed

  • General discussion

  • When you create a DSVM instance and later delete it after using it for some time, several associated Azure resources (like network interfaces, Public IP address etc) are still left behind. The idea is that you can reuse them when you create the VM again (potentially saving time / config etc). But I find in my usage I found I was left with several of these resources on my subscription that I dont really want to reuse and ended up with a long list of unused resources. There are two solutions for cleaning this up:

    Case 1: If you create a new resource group for each DS VM (or set of VMs), you can just delete the resource group and it will remove all the resources under that resource group. Please be careful you dont have any other resources you need within the resource group. In fact, Azure recommends you dont manage resources individually and instead treat them as a resource group and create/delete them in one unit. 

    Case 2: If you tend to create most VMs in one or a few resource groups, you can clean up the unused resources using a Powershell script to find the orphaned resources. For examples find network interfaces that are not associated with any VMs,Public Ip not associated with any network interfaces, Network Security that are not associated. Thank fully someone has already done this. I found a great article at: and all Powershell scripts (as modular functions) are published on Github: (Thanks Alex!)

    Once you download the powershell script to you desktop or another DSVM, you can just dot source the 3 cleanup PS1 scripts in Azure Powershell to add the cleanup functions. You can then them on each resource group and cleanup all unassociated resources :

    Cleanup-AzureRmNetworkInterfaces -ResourceGroup <RG Name> -ListOnly

    Cleanup-AzureRmNetworkInterfaces -ResourceGroup <RG Name> 

    Cleanup-AzureRmPublicIPAddress -ResourceGroup <RG Name> 

    Cleanup-RmNetworkSecurityGroup -ResourceGroup <RG Name> 

    The script prompts before it deletes resource. So there is one extra safety to prevent unintentional deletion of resources. 

    There are a few more resources like storage account of the deleted VM that are not cleaned up by above script. But hopefully this pattern can be used to enhance the cleanup script. 

    Curious to hear what others have done to cleanup Azure resources after deleting  instance(s) of DSVM? 

    Monday, December 5, 2016 11:34 PM