Skip to content

Azure delete VM and child resources using PowerShell

In Azure delete VM is an easy task. Just go to the VM and click “Delete” on the menu. The issue is that only deletes the Virtual Machine resource, it does not delete the dependant child resources such as disk and network interface. Obviously you can manually delete each child resource but that is time consuming and…well manual. This is where PowerShell comes in.

Azure VM

If you don’t have the PowerShell Az module already installed then you can follow this Microsoft document: Install Azure PowerShell

The first job is to connect or login to your Azure account. If you have access to multiple subscriptions you also need to set which subscription to work with. Use the commands below to login and set your subscription scope for the session:

Connect-AzAccount
Get-AzSubscription
Set-AzContext -Subscription "subscriptionId"

Once connected you can press on with Azure delete VM. Firstly get the VM object into a variable: $vm = Get-AzVm -Name "vmName"

Now you have the VM object in a variable, use it to find and stop the VM backup (if one exists). If you don’t do this step then you get backup alerts for failed backups because the VM no longer exists when a backup schedule tries to run.

Foreach ($bkpvlt in Get-AzRecoveryServicesVault) {
  $bkpctr = Get-AzRecoveryServicesBackupContainer -ContainerType AzureVM -VaultId $bkpvlt.ID -FriendlyName $vm.Name
  if ($bkpctr) {
    $vltid = $bkpvlt.ID
  }
}

if ($bkpctr) {
  $bkpitm = Get-AzRecoveryServicesBackupItem -Container $bkpctr -WorkloadType AzureVM -VaultId $vltid
  Disable-AzRecoveryServicesBackupProtection -Item $bkpitm -VaultId $vltid -Force
} else {
  Write-Warning -Message 'No backup found for VM'
}

Now the backup has been stopped (and data retained) proceed to delete VM: $vm | Remove-AzVM -Force

With the VM deleted, you can delete the network adapter and Public IP address (if exists):

Foreach ($nicUri in $vm.NetworkProfile.NetworkInterfaces.Id) {
  $nic = Get-AzNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name $nicUri.Split('/')[-1]
  Remove-AzNetworkInterface -Name $nic.Name -ResourceGroupName $vm.ResourceGroupName -Force
  Foreach ($ipConfig in $nic.IpConfigurations) {
    if($ipConfig.PublicIpAddress -ne $null) {
      Remove-AzPublicIpAddress -ResourceGroupName $vm.ResourceGroupName -Name $ipConfig.PublicIpAddress.Id.Split('/')[-1] -Force
    }
  }
}

Final task, for this post at least is to delete any VM disks. This will delete any managed disks on the VM along with any data disks.

Get-AzDisk | where { $_.ManagedBy -eq $vm.Id } | Remove-AzDisk -Force

if ('DataDiskNames' -in $vm.PSObject.Properties.Name -and @($vm.DataDiskNames).Count -gt 0) {
  Foreach ($uri in $vm.StorageProfile.DataDisks.Vhd.Uri) {
    $dataDiskStorageAcct = Get-AzStorageAccount -Name $uri.Split('/')[2].Split('.')[0]
    $dataDiskStorageAcct | Remove-AzStorageBlob -Container $uri.Split('/')[-2] -Blob $uri.Split('/')[-1]
  }
}

Combine all those commands into a script to quickly and easily Azure delete VM and child resources. Feel free to add more commands to delete other resources too. You could add a check to see if the resource group is left empty post Azure delete VM, if so that could be deleted too.

Published inAzure

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.