---
title: "Delete Azure VMs & Clean Up with PowerShell"
description: "Learn how to delete an Azure VM and clean up related resources using the Remove-AzVM PowerShell command."
canonical: "https://adamtheautomator.com/azure-vm-delete/"
---

# Delete Azure VMs & Clean Up with PowerShell

> Learn how to delete an Azure VM and clean up related resources using the Remove-AzVM PowerShell command.

Source: https://adamtheautomator.com/azure-vm-delete/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![Delete Azure VMs & Clean Up with PowerShell](https://adamtheautomator.com/wp-content/uploads/2019/07/5d238ae5c824b514689ea66d.jpg)

# Delete Azure VMs & Clean Up with PowerShell

[![](https://secure.gravatar.com/avatar/d0b9d42e21e5622713f8b693aa5c0f9244d5f7dd200ed29b8398f52dee5de337?s=192&d=mm&r=g)Adam Bertram](https://adamtheautomator.com/author/adam-bertram/)1 July 20195 min. read

Categories: [Cloud](/category/cloud/)

Tags:[Azure Virtual Machines](/tag/azure-virtual-machines/)[Microsoft Azure](/tag/microsoft-azure/)[PowerShell](/tag/powershell/)

Table of Contents

*   [Prerequisites](#prerequisites)
*   [Getting the Azure VM Object](#getting-the-azure-vm-object)
*   [Removing the Boot Diagnostics Storage Container](#removing-the-boot-diagnostics-storage-container)
*   [Finally Delete an Azure VM](#removing-the-azure-vm)
*   [Removing Network Interfaces and Public IP Addresses](#removing-network-interfaces-and-public-ip-addresses)
*   [Removing the OS Disk](#removing-the-os-disk)
*   [Removing the OS Disk Status Blob](#removing-the-os-disk-status-blob)
*   [Removing Attached Data Disks](#removing-attached-data-disks)
*   [Summary](#summary)

One of the most common uses of the Microsoft Azure cloud for administrators is virtual machines (VMs). It’s easy to create Azure VMs, but it’s not quite as intuitive to remove one. If you need to learn how to delete an Azure VM, you’ve come to the right place.

Not a reader? Watch this related video tutorial!

**_Not seeing the video? Make sure your ad blocker is disabled._**

Sure, you can go to the [Azure Portal](https://portal.azure.com/), and find and delete an Azure VM easily enough. You could also use the `Remove-AzVM` [PowerShell](https://adamtheautomator.com/tag/powershell/) command in the [Azure PowerShell module](https://docs.microsoft.com/en-us/powershell/azure/?view=azps-7.1.0&viewFallbackFrom=azps-2.2.0) to quickly remove a VM but there’s a lot more to that VM than just the VM itself.

When that VM is created, you could have potentially created many other resources along with it that should be cleaned up as well. Resources like the below can all be associated with a VM that needs to be removed.

*   Boot diagnostics storage containers
*   Network interfaces
*   Public IP addresses
*   OS disk and status storage blobs
*   Data disks

In this article, we’ll cover how to build a [PowerShell function](https://adamtheautomator.com/powershell-functions/ "PowerShell function") called `Remove-AzrVirtualMachine` that will not only delete an Azure VM but also anything directly associated with it.

![Delete Azure VM script](https://adamtheautomator.com/wp-content/uploads/2022/04/image-118-1024x526.png)

Delete Azure VM script

## Prerequisites

To delete an Azure VM and cleanup other related resources with PowerShell, you’re going to need a couple of things.

*   The Azure [PowerShell Module](https://adamtheautomator.com/powershell-modules/ "PowerShell Module") – This PowerShell module can be downloaded and installed via the PowerShell Gallery by running `Install-Module Az`.
*   Authenticated to an Azure subscription – You’ll need to be authenticated to the Azure subscription your VM is located in. To do this, run [`Connect-AzAccount`](https://adamtheautomator.com/connect-azaccount/ "Connect-AzAccount").

Related:[Connect-AzAccount: Your Gateway To Azure with PowerShell](https://adamtheautomator.com/connect-azaccount/)

To verify, you can then run `Get-AzVm -Name <VMName> -ResourceGroupName <VMResourceGroupName>` to check if it returns the VM. If no error is returned, you should be good to go.

## Getting the Azure VM Object

To make things easier in this script, to delete an Azure VM, we’ll first get the VM object that contains all of the necessary information we’re looking for. To do that, we use the `Get-AzVm` command. For this article, I’ll be working with a VM called _WINSRV19_ in the _MyTestVMs_ resource group.

```powershell
$vm = Get-AzVm -Name WINSRV19 -ResourceGroupName MyTestVMs
```

You should now have the VM object stored in the `$vm` variable allowing you to read various objects from the variable as you progress through the script.

## Removing the Boot Diagnostics Storage Container

When creating an Azure VM, you always have the option of creating a boot diagnostics container. This is useful for troubleshooting VM boot issues but doesn’t get removed when a VM is deleted. Let’s remedy that.

To remove the boot diagnostics container, you first need to figure out the name of the storage account the container resides. To find this storage account, you’ll have to do some parsing of the `storageUri` property that’s buried in the `DiagnosticsProfile` object on the VM.

To save you some time, here’s some regex to parse out that name.

```powershell
$diagSa = [regex]::match($vm.DiagnosticsProfile.bootDiagnostics.storageUri, '^http[s]?://(.+?)\\.').groups[1].value
```

Next, you’ll need to find the name of the boot diagnostics storage container. To find that, you’ll first need to find the VM ID using the `Get-AzResource` command. Here is some PowerShell to get that done for you.

```powershell
if ($vm.Name.Length -gt 9) {
    $i = 9
} else {
    $i = $vm.Name.Length - 1
}

$azResourceParams = @{
    'ResourceName' = WINSRV
    'ResourceType' = 'Microsoft.Compute/virtualMachines'
    'ResourceGroupName' = MyTestVMs
}

$vmResource = Get-AzResource @azResourceParams
$vmId = $vmResource.Properties.VmId
$diagContainerName = ('bootdiagnostics-{0}-{1}' -f $vm.Name.ToLower().Substring(0, $i), $vmId)
```

Next, you’ll need the name of the resource group the boot diagnostics container is a part of.

```powershell
$diagSaRg = (Get-AzStorageAccount | where { $_.StorageAccountName -eq $diagSa }).ResourceGroupName
```

Finally, you can now remove the storage container with the `Remove-AzStorageContainer` command.

```powershell
$saParams = @{
    'ResourceGroupName' = $diagSaRg
    'Name' = $diagSa
}

Get-AzStorageAccount @saParams | Get-AzStorageContainer | where { $_.Name-eq $diagContainerName } | Remove-AzStorageContainer -Force
```

Related: [Getting to Know the PowerShell Pipeline and Creating Functions](https://adamtheautomator.com/ppowershell-pipeline/)

One resource down! Let’s keep going and continue on how to delete an Azure virtual machine from disk.

## Finally Delete an Azure VM

Next, we’ll delete an Azure VM itself. Since you’ve already captured the VM in the `$vm` variable, you can pass that object to the `Remove-AzVm` command and be done with it.

```powershell
$null = $vm | Remove-AzVM -Force
```

## Removing Network Interfaces and Public IP Addresses

Next up, are one or more network interfaces (NICs) that were once attached to that VM. Using the `NetworkInterfaces` property on the VM object, we can loop through each ID, remove the NIC with the `Remove-AzNetworkInterface` command.

While we’re in this loop, we can also check the `IpConfiguration` property on each NIC to discover if that NIC has a public IP address associated with it. If so, we can remove that with the `Remove-AzPublicIpAddress` command.

Below you can see, I’m looking for all NICs, removing each one, checking for public IPs on each NIC and if found, parsing the `PublicIpAddress` property’s ID to get the name of the public IP address resource and remove it.

```powershell
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
        }
    }
}
```

## Removing the OS Disk

To delete an Azure VM, the OS disk isn’t removed. The OS disk is a storage blob you can remove with the `Remove-AzStorageBlob` command but first, like the other steps, you’ll have to do some searching to find the correct parameter values to pass to it.

In the below example PowerShell code snippet, I’m first finding the name of the OS disk storage container. Once I find that I’m then using that along with the storage account the OS disk storage container resides on to pass to `Remove-AzStorageBlob` to get the OS disk removed.

```powershell
$osDiskUri = $vm.StorageProfile.OSDisk.Vhd.Uri
$osDiskContainerName = $osDiskUri.Split('/')[-2]
$osDiskStorageAcct = Get-AzStorageAccount | where { $_.StorageAccountName -eq $osDiskUri.Split('/')[2].Split('.')[0] }
$osDiskStorageAcct | Remove-AzStorageBlob -Container $osDiskContainerName -Blob $osDiskUri.Split('/')[-1]
```

## Removing the OS Disk Status Blob

To clean up the OS disk status blog, I need to find the storage container the OS disk is in, assume the blog itself ends in status, and then pass that to `Remove-AzStorageBlob` to obliterate that blob.

```powershell
$osDiskStorageAcct | Get-AzStorageBlob -Container $osDiskContainerName -Blob "$($vm.Name)*.status" | Remove-AzStorageBlob
```

## Removing Attached Data Disks

Finally, some VMs may have data disks attached to them that you’d rather not use elsewhere. Not a problem. To delete an Azure VM properly, we can remove those too! This is the final step in deleting an Azure VM and all of its components.

First, you’ll read the `Uri` property embedded deep in the `StorageProfile` object on the original VM object you retrieved. Since there can be more than one data disk with a URI, you’ll loop over each of these URIs to find the storage account each one is on.

Once you’ve discovered the storage account of each one, you’ll use that and parse the storage URI to form the blob name to pass to the `Remove-AzStorageBlob` command, as shown below.

```powershell
if ($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]
    }
}
```

## Summary

We’ve covered quite a bit in this article. Following best PowerShell coding practices, you could quickly build a tool from this code. Lucky for you, I’ve already done it for you! If you’d like to see an example of how this code could be implemented to delete an Azure VM, check out the [Remove-AzrVirtualMachine function](https://github.com/adbertram/Random-PowerShell-Work/blob/master/Azure/Remove-AzrVirtualMachine.ps1).

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fazure-vm-delete%2F&text=Delete%20Azure%20VMs%20%26%20Clean%20Up%20with%20PowerShell)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fazure-vm-delete%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fazure-vm-delete%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2019/12/network-2402637_1280.jpg)

### [Optimize Azure Scale Sets Management with PowerShell](/azure-powershell-scale-set/)

Master Azure Scale Sets management using PowerShell. Learn to create, configure, and automate VM scale sets for improved cloud infrastructure.

![](https://adamtheautomator.com/wp-content/uploads/2019/07/iot-3337536_1280.png)

### [Run Scripts on Azure VMs with Custom Script Extension](/azure-run-script-on-vm/)

Remotely run PowerShell scripts on your Azure VMs using the Azure custom script extension for Windows.

![](https://adamtheautomator.com/wp-content/uploads/2019/07/5d238ae5c824b514689ea64e.jpg)

### [Effortless Azure VM Snapshots with PowerShell](/azure-vm-snapshot/)

Create, manage, restore, and remove Azure VM snapshots using a community PowerShell module in just a few keystrokes!

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
