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 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.

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

Search for Virtual machines there.

How to remove an Azure Virtual Machine(VM)

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 remove an Azure Virtual Machine(VM)

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

How to remove an Azure Virtual Machine

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

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?

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?

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?

Step-5. 

Click on “Edit” from your Virtual machine disks screen

How do I detach data disk from Azure VM in Azure portal

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

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

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

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

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

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 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, 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

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.

You may like following Azure tutorials:

Conclusion

In this Azure tutorial, We discussed

  • 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

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

Leave a Comment