none
Failed to delete storage container 'vhds'

    Question

  • I've Got the old

    "Failed to delete storage container 'vhds'. Error: There is currently a lease on the container and no lease ID was specified in the request."

    message when trying to delete a ARM (new portal based storage account)

    NOTE I know this has been asked before and on StackOverflow, so before anyone tries to answer this be aware the VM I created was done in the new portal and it was deleted weeks ago. This means the old trick of looking for images in the classic portal won't work and there are none in my subscription.

    The UK based answer that shows me how to do this in ARM based PowerShell gets a PowerShell T-shirt if that helps
    Monday, March 7, 2016 12:28 PM

Answers

  • OK I wrote this by adapting it for the TechNet Gallery as I got fed up with waiting..

    [CmdletBinding(SupportsShouldProcess = $true)]
    
    Param
    (
        [Parameter(Mandatory=$true)]
        [Alias('SN')]
        [String]$StorageAccountName,
        [Parameter(Mandatory=$true)]
        [Alias('CN')]
        [String]$ContainerName,
        [Parameter(Mandatory=$true)]
        [Alias('BN')]
        [String]$BlobName,
        [Parameter(Mandatory=$true)]
        [String]$ResourceGroupName
    )
    
    #Check if Windows Azure PowerShell Module is avaliable
    If((Get-Module -ListAvailable Azure) -eq $null)
    {
        Write-Warning "Windows Azure PowerShell module not found! Please install from http://www.windowsazure.com/en-us/downloads/#cmd-line-tools"
    }
    Else
    {
        If($StorageAccountName)
        {
            $Storage = Get-AzureRMStorageAccount -Name $StorageAccountName `
            -ErrorVariable IsExistStorageError -ResourceGroupName $ResourceGroupName 
    
            #Check if storage account is exist
            If($IsExistStorageError.Exception -eq $null)
            {
                If($ContainerName)
                {
                    azure.storage\Get-AzureStorageContainer -Name $ContainerName -ErrorAction SilentlyContinue -Context $Storage.Context  `
                    -ErrorVariable IsExistContainerError | Out-Null
    
                    #Check if container is exist
                    If($IsExistContainerError.Exception -eq $null)
                    {
                        If($BlobName)
                        {
                            azure.storage\Get-AzureStorageBlob -Container $ContainerName -Blob $BlobName -ErrorAction SilentlyContinue -Context $storage.Context `
                            -ErrorVariable IsExistBlobError | Out-Null
    
                            #Check if blob is exist
                            If($IsExistBlobError.Exception -eq $null)
                            {
                                #Specify a Windows Azure Storage Library path
                                $StorageLibraryPath = "C:\Program Files\Microsoft SDKs\Azure\.NET SDK\v2.8\ToolsRef\Microsoft.WindowsAzure.Storage.dll"
    
                                #Getting Azure storage account key
                                $Keys = Get-AzureRmStorageAccountKey -StorageAccountName $StorageAccountName -ResourceGroupName $ResourceGroupName
                                $StorageAccountKey = $Keys.Key1
    
                                #Loading Windows Azure Storage Library for .NET.
                                Write-Verbose -Message "Loading Windows Azure Storage Library from $StorageLibraryPath"
                                [Reflection.Assembly]::LoadFile("$StorageLibraryPath") | Out-Null
    
                                $Creds = New-Object Microsoft.WindowsAzure.Storage.Auth.StorageCredentials("$StorageAccountName","$StorageAccountKey")
                                $CloudStorageAccount = New-Object Microsoft.WindowsAzure.Storage.CloudStorageAccount($creds, $true)
                                $CloudBlobClient = $CloudStorageAccount.CreateCloudBlobClient()
    
                                Write-Verbose "Getting the container object named $ContainerName."
                                $BlobContainer = $CloudBlobClient.GetContainerReference($ContainerName)
    
                                $Blob = $BlobContainer.ListBlobs() | Where{$_.Name -eq $BlobName}
    
                                If($Blob.Properties.LeaseStatus -eq "Locked")
                                {
                                    Try
                                    {
                                        Write-Verbose "Breaking leases on '$BlobName' blob."
                                        $Blob.BreakLease($(New-TimeSpan), $null, $null, $null) | Out-Null
                                        Write-Host "Successfully broken lease on '$BlobName' blob."
                                    }
                                    Catch
                                    {
                                        Write-Host "Failed to break lease on '$BlobName' blob." -ForegroundColor Red
                                    }
                                }
                                Else
                                {
                                    Write-Host "The '$BlobName' blob's lease status is unlocked."
                                }
                            }
                            Else
                            {
                                Write-Warning "Cannot find blob '$BlobName' because it does not exist. Please make sure thar the name of blob is correct."
                            }
                        }
                    }
                    Else
                    {
                        Write-Warning "Cannot find container '$ContainerName' because it does not exist. Please make sure thar the name of container is correct."
                    }
                }
            }
            Else
            {
                Write-Warning "Cannot find storage account '$StorageAccountName' because it does not exist. Please make sure thar the name of storage is correct."
            }
        }
    }

    Monday, March 7, 2016 1:58 PM

All replies

  • Is there anything still in the container?

    What do you get if you run:
    Azure.Storage\Get-AzureStorageBlob -Context $storage.Context -Container "vhds"
    Where $storage is a variable containing the storage account.

    The first step is to ensure it is empty. If it isn't a lease may exist to a blob within the container.

    Marcus :-)

    Twitter: @techdiction Blog: http://www.techdiction.com

    Monday, March 7, 2016 12:35 PM
    Moderator
  • I have the same problem. So I try to delete this vhd file in storage container and got this error:

    "ESCRIPTIONFailed to delete blob 'vhds/VS13-WIN81-64X-20140913-170499-os-2014-09-13.vhd'. Error: There is currently a lease on the blob and no lease ID was specified in the request.

    STATUSError
    TIMEMonday, March 7, 2016, 1:35:25 PM
    CORRELATION IDSclientNotification-a39e4f74-31c9-4a65-a4fe-71a75083ffff"

    Monday, March 7, 2016 12:43 PM
  • Has the VM been deleted? You can try running this script, although I haven't tested using Resource Manager:

    https://gallery.technet.microsoft.com/How-to-break-the-locked-c2cd6492

    Twitter: @techdiction Blog: http://www.techdiction.com

    Monday, March 7, 2016 1:16 PM
    Moderator
  • OK I wrote this by adapting it for the TechNet Gallery as I got fed up with waiting..

    [CmdletBinding(SupportsShouldProcess = $true)]
    
    Param
    (
        [Parameter(Mandatory=$true)]
        [Alias('SN')]
        [String]$StorageAccountName,
        [Parameter(Mandatory=$true)]
        [Alias('CN')]
        [String]$ContainerName,
        [Parameter(Mandatory=$true)]
        [Alias('BN')]
        [String]$BlobName,
        [Parameter(Mandatory=$true)]
        [String]$ResourceGroupName
    )
    
    #Check if Windows Azure PowerShell Module is avaliable
    If((Get-Module -ListAvailable Azure) -eq $null)
    {
        Write-Warning "Windows Azure PowerShell module not found! Please install from http://www.windowsazure.com/en-us/downloads/#cmd-line-tools"
    }
    Else
    {
        If($StorageAccountName)
        {
            $Storage = Get-AzureRMStorageAccount -Name $StorageAccountName `
            -ErrorVariable IsExistStorageError -ResourceGroupName $ResourceGroupName 
    
            #Check if storage account is exist
            If($IsExistStorageError.Exception -eq $null)
            {
                If($ContainerName)
                {
                    azure.storage\Get-AzureStorageContainer -Name $ContainerName -ErrorAction SilentlyContinue -Context $Storage.Context  `
                    -ErrorVariable IsExistContainerError | Out-Null
    
                    #Check if container is exist
                    If($IsExistContainerError.Exception -eq $null)
                    {
                        If($BlobName)
                        {
                            azure.storage\Get-AzureStorageBlob -Container $ContainerName -Blob $BlobName -ErrorAction SilentlyContinue -Context $storage.Context `
                            -ErrorVariable IsExistBlobError | Out-Null
    
                            #Check if blob is exist
                            If($IsExistBlobError.Exception -eq $null)
                            {
                                #Specify a Windows Azure Storage Library path
                                $StorageLibraryPath = "C:\Program Files\Microsoft SDKs\Azure\.NET SDK\v2.8\ToolsRef\Microsoft.WindowsAzure.Storage.dll"
    
                                #Getting Azure storage account key
                                $Keys = Get-AzureRmStorageAccountKey -StorageAccountName $StorageAccountName -ResourceGroupName $ResourceGroupName
                                $StorageAccountKey = $Keys.Key1
    
                                #Loading Windows Azure Storage Library for .NET.
                                Write-Verbose -Message "Loading Windows Azure Storage Library from $StorageLibraryPath"
                                [Reflection.Assembly]::LoadFile("$StorageLibraryPath") | Out-Null
    
                                $Creds = New-Object Microsoft.WindowsAzure.Storage.Auth.StorageCredentials("$StorageAccountName","$StorageAccountKey")
                                $CloudStorageAccount = New-Object Microsoft.WindowsAzure.Storage.CloudStorageAccount($creds, $true)
                                $CloudBlobClient = $CloudStorageAccount.CreateCloudBlobClient()
    
                                Write-Verbose "Getting the container object named $ContainerName."
                                $BlobContainer = $CloudBlobClient.GetContainerReference($ContainerName)
    
                                $Blob = $BlobContainer.ListBlobs() | Where{$_.Name -eq $BlobName}
    
                                If($Blob.Properties.LeaseStatus -eq "Locked")
                                {
                                    Try
                                    {
                                        Write-Verbose "Breaking leases on '$BlobName' blob."
                                        $Blob.BreakLease($(New-TimeSpan), $null, $null, $null) | Out-Null
                                        Write-Host "Successfully broken lease on '$BlobName' blob."
                                    }
                                    Catch
                                    {
                                        Write-Host "Failed to break lease on '$BlobName' blob." -ForegroundColor Red
                                    }
                                }
                                Else
                                {
                                    Write-Host "The '$BlobName' blob's lease status is unlocked."
                                }
                            }
                            Else
                            {
                                Write-Warning "Cannot find blob '$BlobName' because it does not exist. Please make sure thar the name of blob is correct."
                            }
                        }
                    }
                    Else
                    {
                        Write-Warning "Cannot find container '$ContainerName' because it does not exist. Please make sure thar the name of container is correct."
                    }
                }
            }
            Else
            {
                Write-Warning "Cannot find storage account '$StorageAccountName' because it does not exist. Please make sure thar the name of storage is correct."
            }
        }
    }

    Monday, March 7, 2016 1:58 PM