Delete Azure VM

This Azure tutorial will discuss how to delete VM in Azure.

Delete Azure VM

You can remove or delete VM in Azure if you don’t need it. You can delete Azure VM using the Azure portal easily.

  1. Login to https://portal.azure.com/
  2. Search for Virtual machines there.
how to delete vm in azure

You will see a list of VMs created in your Azure subscription. It will show the VM name, Type, Status, Resource Group, Location, etc.

az vm delete

3. Now tick the virtual machine you want to delete and click the Delete button.

how to delete vm in azure

4. Now, in the Delete Resources window, type Yes in the Confirm delete option and then select the Delete button.

how to delete azure vm and all associated resources

Delete VM In Azure CLI

You can also use the Azure CLI to delete a virtual machine like PowerShell. You can use az vm delete cmdlet.

az vm delete

You can use the az vm delete Azure CLI cmdlet to delete a virtual machine very quickly.

Syntax:

Below is the syntax for the az vm delete

az vm delete [--force-deletion]
 [--ids]
 [--name]
 [--no-wait]
 [--resource-group]
 
[--subscription]
 [--yes]

Example:

Let’s discuss an example of using the Azure CLI cmdlet az vm delete to delete an Azure Virtual machine.

az vm delete -g DemoResourceGroup -n TSINFOVm --yes

DemoResourceGroup is the name of my resource group and TSINFOVm is the virtual machine name.

Once you execute the above Azure CLI cmdlet, the Azure Virtual Machine will be deleted in a few seconds.

If you want to delete all the virtual machines in a resource group, You can use the below Azure CLI PowerShell cmdlet

az vm delete --ids $(az vm list -g DemoResourceGroup --query "[].id" -o tsv)

Remove-AzVM

The remove-azvm is an excellent Azure PowerShell cmdlet that can quickly remove the Virtual Machine.

Syntax:

Below is the syntax for the Remove-AzVM Azure PowerShell cmdlet.

Remove-AzVM [-Name] <String> [-Force] [-NoWait][-ResourceGroupName] <String>
[-AsJob] [-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm]
[<CommonParameters>]

Example

Let’s discuss an example of using the Remove-AzVM Azure PowerShell cmdlet to delete the Azure Virtual Machine.

Remove-AzVM -ResourceGroupName "DemoRG1" -Name "TSInfoVM"

Above, “DemoRG1” is the resource group name and “TSInfoVM” is the name of my Virtual machine.

Once you execute the above Azure PowerShell cmdlet, the Azure Virtual Machine will be deleted in seconds. This is one of the best ways to remove the Azure Virtual Machine quickly.

Remove Azure VM using PowerShell

If you want to delete an Azure VM, Azure won’t automatically remove the associated resources like below

  • Diagnostics disk storage container
  • The operating system disk
  • The Nic
  • The operating system disk
  • Data disks

So, before deleting the virtual machine, it is better to remove these dependencies.

First, log in to AzureRmAccount  using the Login-AzureRmAccount  command in PowerShell

C:\windows\system32> Login-AzureRmAccount
delete vm azure

Now, you can use the below cmdlet to delete the different dependent objects before removing the virtual machine(VM).

$myVmName = 'MyNewVM'
 $myrsgName = 'newresgroup'
 $myvm = Get-AzureRmVM –Name $myVmName –ResourceGroupName $myrsgName

 $myResource = @{
 'ResourceName' = $myVmName
 'ResourceType' = 'Microsoft.Compute/virtualMachines'
     'ResourceGroupName' = $myrsgName
 }
 $myvmResource = Get-AzureRmResource @myResource
 $myvmId = $vmResource.Properties.VmId

Removing Diagnostics Storage Container PowerShell

You can use the below cmdlet with regular expression to remove the Diagnostics Storage Container PowerShell

$ds = [regex]::match($myvm.DiagnosticsProfile.bootDiagnostics.storageUri,'^http[s]?://(.+?)\.').groups[1].value
 $ContName = ('bootdiagnostics-{0}-{1}' -f $myvm.Name.ToLower().Substring(0, 9), $myvmId)
 $dg = (Get-AzureRmStorageAccount | where { $_.StorageAccountName -eq $ds }).ResourceGroupName
 $Parameter = @{
 'ResourceGroupName' = $dg
     'Name' = $ds
 }            
 Get-AzureRmStorageAccount @Parameter | Get-AzureStorageContainer | where { $_.Name-eq $ContName } | Remove-AzureStorageContainer –Force

Now, we can remove the Azure virtual machine using PowerShell. Use the below cmdlet

$null = $myvm | Remove-AzVM -Force

Removing Network Interfaces and Public IP Addresses PowerShell

We can use the below cmdlet to remove Network Interfaces and Public IP Addresses using PowerShell

foreach($mynicUri in $myvm.NetworkProfile.NetworkInterfaces.Id) {
    $mynic = Get-AzNetworkInterface -ResourceGroupName $myvm -Name $mynicUri.Split('/')[-1]
    Remove-AzNetworkInterface -Name $mynic.Name -ResourceGroupName $myvm.ResourceGroupName -Force

    foreach($myipConfig in $mynic.IpConfigurations) {
        if($myipConfig.PublicIpAddress -ne $null) {
            Remove-AzPublicIpAddress -ResourceGroupName $myvm.ResourceGroupName -Name $myipConfig .PublicIpAddress.Id.Split('/')[-1] -Force
        }
    }
}

Removing the OS Disk PowerShell

You can use PowerShell to remove the OS disk in Azure VM using the below cmdlet.

$mynewosDiskUri = $myvm.StorageProfile.OSDisk.Vhd.Uri
$diskContainer = $mynewosDiskUri.Split('/')[-2]
$diskStorageAcct = Get-AzStorageAccount | where { $_.StorageAccountName -eq $mynewosDiskUri.Split('/')[2].Split('.')[0] }
$diskStorageAcct | Remove-AzStorageBlob -Container $diskContainer -Blob $mynewosDiskUri.Split('/')[-1]

Removing Attached Data Disks PowerShell

Using PowerShell, you can use the cmdlet below to remove the attached data disks (see how to delete Azure VM disk).

if ($myvm.DataDiskNames.Count -gt 0) {
    foreach ($myuri in $myvm.StorageProfile.DataDisks.Vhd.Uri) {
        $diskStorageAcct = Get-AzStorageAccount -Name $myuri.Split('/')[2].Split('.')[0]
        $diskStorageAcct | Remove-AzStorageBlob -Container $myuri.Split('/')[-2] -Blob $myuri.Split('/')[-1]
    }
}

This is the way how to delete Azure VM and all associated resources.

How To Delete Azure VM and All Associated Resources PowerShell

If you want to delete Azure VM and all associated resources using PowerShell at once, You can do that quickly using a PowerShell Script. Check out the PowerShell script now.

Conclusion

In this Azure tutorial, We discussed how to delete VM in Azure.