How to remove an Azure Virtual Machine (VM)

In this azure tutorial, we will discuss How to remove an Azure Virtual Machine (VM). Apart from this, we will also discuss on the below topics

  • How do I detach data disk from Azure VM?
  • How to delete public IP in Azure VM
  • How to delete public IP in Azure VM using PowerShell
  • How to remove an Azure Virtual Machine (VM) using PowerShell?
  • Removing Diagnostics Storage Container PowerShell
  • Removing the Azure VM PowerShell
  • Removing Network Interfaces and Public IP Addresses PowerShell
  • Removing the OS Disk PowerShell
  • Removing Attached Data Disks PowerShell
  • How to delete virtual machine in azure portal
  • Which OS VM Creation Is Supported In Microsoft Azure
  • Remove-AzVM
  • Delete VM In Azure CLI
  • How To Delete Azure VM and All Associated Resources PowerShell

How to remove an Azure Virtual Machine (VM)

You can remove or delete your virtual machine (VM) if you don’t need it. You can remove an Azure Virtual Machine using the Azure portal easily.

How To Delete A VM In Azure

Login to https://portal.azure.com/

Search for Virtual machines there.

How to remove an Azure Virtual Machine(VM)
how to delete virtual machine in azure portal

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

delete azure vm
how to detach disk from azure vm

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

How to remove an Azure Virtual Machine
azure detach network interface

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

How to delete an Azure Virtual Machine
azure vm delete

How do I detach data disk from Azure VM?

We can detach data disk from the Azure virtual machine by following the below steps.

Step- 1: 

Login to https://portal.azure.com/

Step- 2: 

Search for Virtual machines there.

How do I detach data disk from Azure VM?
azure detach disk

Step- 3: 

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

How do I detach data disk from Azure VM?
remove disk from azure vm

Step-4:

Now click on the virtual machine for which one you want to detach the data disk and click on the Disks link from the left side menu.

How do I detach data disk from Azure VM?
detach os disk from azure vm

Step-5. 

Click on “Edit” from your Virtual machine disks screen

How do I detach data disk from Azure VM in Azure portal
how to detach disk from azure vm

Step 5.  click on the below detach button which is highlighted. It will detach the disk from the virtual machine.  You cannot do any files or shares after that.

how to delete azure vm and all associated resources
how to remove public ip from azure vm

This is how do I remove a disk from an Azure Virtual Machine?

Delete public IP in Azure VM

Here We will discuss How to dissociate public IP in Azure VM or We can delete public IP in Azure virtual machine by following the below steps.

Step- 1: 

Login to https://portal.azure.com/

Step- 2: 

Search for Virtual machines there.

dissociate public ip in azure vm
azure vm remove public ip

Step- 3: 

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

how to dissociate public ip in azure vm
azure detach network interface

Step- 4: 

Now click on the virtual machine it will open the Overview tab. Click on the Public IP address.

How to delete public ip in azure vm
azure remove public ip from vm

Step- 5:

Now from the Public IP address page and click on the Dissociate button or Delete button then select Yes for the confirmation.

How to dissociate public ip in azure vm
remove public ip from azure vm

How to delete public IP in Azure VM using PowerShell

Here We will discuss How to dissociate public IP in Azure VM or We can delete public IP in Azure virtual machine using PowerShell by following the below steps.

Step-1:

Sign in to Azure with Connect-AzAccount

PS C:\windows\system32> Connect-AzAccount

Step-2:

Execute the below cmdlet then after

 $mynic = Get-AzNetworkInterface -Name MyNewAZVMNic -ResourceGroup newresgroup
 $mynic.IpConfigurations.publicipaddress.id = $null
 Set-AzNetworkInterface -NetworkInterface $mynic
How to delete public IP in Azure VM using PowerShell
remove public ip from azure vm

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

Delete Virtual Machine Azure PowerShell

So before deleting the virtual machine, Better to remove these dependencies.

First login to AzureRmAccount  using the Login-AzureRmAccount  command in PowerShell

C:\windows\system32> Login-AzureRmAccount
How to remove an Azure Virtual Machine(VM) using PowerShell
detach os disk from azure vm

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

Removing the Azure VM PowerShell

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 the below cmdlet to remove the OS disk in Azure VM using PowerShell.

$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

You can use the below cmdlet to remove the attached data disks (how to delete azure VM disk) using PowerShell.

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.

Which OS VM Creation Is Supported In Microsoft Azure

Note that, As of now, You can create Windows OS and Linux OS virtual machines in Microsoft Azure.

Remove-AzVM

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

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 will execute the above Azure PowerShell cmdlet, it will take a span of seconds to delete the Azure Virtual Machine. This is one of the best ways to remove the Azure Virtual machine very quickly.

Delete VM In Azure CLI

Like PowerShell, you can also use the Azure CLI to delete a virtual machine. 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 will execute the above Azure CLI cmdlet, it will take a few seconds to delete the Azure Virtual Machine.

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)

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 virtual machine in azure portal
  • How do I detach data disk from Azure VM?
  • How to delete public IP in Azure VM
  • How to delete public IP in Azure VM using PowerShell
  • How to remove an Azure Virtual Machine (VM) using PowerShell?
  • Removing Diagnostics Storage Container PowerShell
  • Removing the Azure VM PowerShell
  • Removing Network Interfaces and Public IP Addresses PowerShell
  • Removing the OS Disk PowerShell
  • Removing Attached Data Disks PowerShell
  • Which OS VM Creation Is Supported In Microsoft Azure
  • Remove-AzVM
  • Delete VM In Azure CLI
  • How To Delete Azure VM and All Associated Resources PowerShell

I hope this tutorial, we learned how to remove azure virtual machine (VM) from the Azure portal as well as using PowerShell.