A few months ago I wrote a blog post on Deleting an Azure Hard Drive with PowerShell.

This is great if you just have a drive or two you want to delete. I was doing more testing with premium storage and had 32 drives on one VM that I needed to delete (more on why in a future post). The GUI still wasn’t working for me, and the steps in this post were just going to take too long. So, why not write a PowerShell script to do it all at once. So, here it is:

$vmservice = Get-AzureVM -ServiceName $ServiceName
$vm = $vmservice.VM | ? {$_.RoleName -eq $VMName}
$disks = $vm.DataVirtualHardDisks
for($i = 0; $i -lt $disks.Count){
Remove-AzureDataDisk -Lun $disks[$i].LUN -VM $vm -DeleteVHD
Update-AzureVM -ServiceName $vmservice.Name -VM $vm -Name $vm.RoleName

$ServiceName is the name of the Service your VM is under and $VMName is is the name of your VM. I know, pretty self explanatory. You can get these two strings by running Get-AzureVM and finding the ServiceName and Name in the list that is returned.

I’ve created a function for this script as well that I’ve posted up to GitHub here – https://github.com/benstegink/PowerShellScripts/blob/master/Azure/Delete-AzureAllVMDataDisks.ps1. Just a fair warning, while this automates the whole process, it can still take a while to run. To delete the 32 drives I mentioned above it took the script a little over 45 minutes to run. There are no status updates, but if you want the Data Drives in the new Azure Portal – https://portal.azure.com you’ll slowly see the drives drop off the list.