2

I have an Azure VM and I'm trying to delete it using Powershell. I also want to remove the disk that that VM OS was on (there are no data disks). I assume I'm going to need the following two cmdlets:

  • Remove-AzureVM
  • Remove-AzureDisk

Here's my code:

$VMs = Get-AzureVM $svcName
foreach ($VM in $VMs)
{
    $OSDisk = ($VM | Get-AzureOSDisk)
    if ($VM.InstanceStatus -eq "ReadyRole") {
        Stop-AzureVM -Name $VM.Name -ServiceName $svcName
    }
    remove-azurevm -ServiceName $svcName -Name $VM.Name
    Remove-AzureDisk -DiskName $OSDisk.DiskName
}

When I execute this the call to Remove-AzureVM returns successfully but the call to Remove-AzureDisk returns an error:

Remove-AzureDisk : BadRequest: A disk with name XXX is currently in use by virtual machine YYY running within hosted service ZZZ, deployment XYZ.

Strange thing is, I can issue the same call to Remove-AzureDisk just a few moments later and it returns successfully.

Its as if the call to Remove-AzureVM is returning too quickly. i.e. Its reporting success before the VM has been fully removed, or before the link to the disk has been removed at any rate.

Can anyone explain why this might be and also how I might work around this problem?

thanks in advance

Jamie

jamiet
  • 10,501
  • 14
  • 80
  • 159

4 Answers4

2

What's happening here is that the Disk that is stored in BLOB storage is locked when in use by a VM. You are removing the VM, but it takes a few moments for the Lease on the BLOB to release. That's why you can remove it a few moments later.

There are a few folks who have written PowerShell to break the lease, or you could use PowerShell to use the SDK (or make direct REST API calls) to check lease status.

MikeWo
  • 10,887
  • 1
  • 38
  • 44
  • Thanks Mike, I figured that might be the case. Also good to know that the correct nomenclature is "lease". If anyone reading can share any code that will break the lease then I'd be very grateful if you could share it. Thx. – jamiet Oct 29 '13 at 06:34
  • 1
    You can see how they did it with PowerShell using the storage SDK on this TechNet article: http://technet.microsoft.com/en-us/library/jj919145.aspx You'll need to adapt it as it's doing some things you won't care about. – MikeWo Oct 29 '13 at 12:05
0

I ended up writing a script that creates a VM, clones it, then deletes the clones. As part of that I needed to wait until the lease was released hence if you're experiencing this same problem you might want to check my blog post at http://sqlblog.com/blogs/jamie_thomson/archive/2013/11/04/clone-an-azure-vm-using-powershell.aspx as it'll have some code that might help you.

Regards

Jamie

jamiet
  • 10,501
  • 14
  • 80
  • 159
0

I puzzled at this for quite a while. Ultimately, I found a different command to do what I thought I was doing with this command. I would recommend the remove-azuredatadisk command to delete a disk, as it automatically breaks the lease.

Get-AzureVM -ServiceName <servicename> -name <vmname> |Remove-AzureDataDisk -lun <lun#> -deletevhd | Update-AzureVM

It will spin for a couple of minutes, but it will give you a success/failure output at the end.

This command just does it, and doesn't give you any feedback about which drive was removed. I would recommend tossing in a get-azuredatadisk first just to be sure of what you deleted.

Get-AzureVM -ServiceName <servicename> -name <vmname> | Get-AzureDataDisk
Fabio Antunes
  • 22,251
  • 15
  • 81
  • 96
0

This is related to Windows Azure: Delete disk attached to non-existent VM. Cross-posting my answer here:

I was unable to use the (2016) web portal to delete orphaned disks in my (classic) storage account. Here is a detailed walk-through for deleteing these orphaned disks with PowerShell.

PowerShell

Download and install PowerShell if you haven't already. (Install and configure Azure PowerShell.) Initial steps from this doclink:

  • Check that the Azure PowerShell module is available after installing:
    Get-Module –ListAvailable

  • If the Azure PowerShell module is not listed, you may need to import it:
    Import-Module Azure

  • Login to Azure Resource Manager:
    Login-AzureRmAccount

AzurePublishSettingsFile

  • Retreive your PublishSettingsFile.
    Get-AzurePublishSettingsFile

  • Import-AzurePublishSettingsFile and specify the path to the file just saved.
    Import-AzurePublishSettingsFile -PublishSettingsFile '<your file path>'

Show and Remove Disks

Community
  • 1
  • 1
Michelle Welcks
  • 3,513
  • 4
  • 21
  • 34