Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to break a lease on Blob Storage in Azure with PowerShell?

How do I break a lease on an item in Blob Storage utilizing PowerShell?

I'm receiving the following when trying to upload something over the current image:

Add-AzureRmVhd : The remote server returned an error: (412) There is currently a lease on the blob and no lease ID was specified in the request..
At line:1 char:1
+ Add-AzureRmVhd -Destination $osDiskUri -LocalFilePath $localFileName  ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Add-AzureRmVhd], StorageException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.Compute.StorageServices.AddAzureVhdCommand
like image 420
Doug Avatar asked Mar 10 '16 15:03

Doug


3 Answers

Login to the old portal and navigate to the Virtual Machines then the Images tab the url will be https://manage.windowsazure.com/@yourname.onmicrosoft.com#Workspaces/VirtualMachineExtension/images. Select the image and choose Delete on the bottom.

enter image description here

After that go to your storage and delete it.

You can also try the following which will remove blobs for a given container and then remove the container.

Add-AzureAccount
Get-AzureSubscription | Format-Table SubscriptionName, IsDefault, IsCurrent, CurrentStorageAccountName

$SubscriptionName = 'Your subsscription name'
Select-AzureSubscription -SubscriptionName $SubscriptionName

Get-AzureSubscription -Default

Get-AzureStorageAccount | Format-Table -Property StorageAccountName, Location, AccountType, StorageAccountStatus

$StorageAccountName = "Your storage account"
$StorageAccountKey = (Get-AzureStorageKey -StorageAccountName $StorageAccountName).Primary
$ContainerName = "Your container name"
$Context = New-AzureStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey

#Get a reference to all the blobs in the container.
$blobs = Get-AzureStorageBlob -Container $ContainerName -Context $Context

#Remove lease on each Blob
$blobs | %{$_.ICloudBlob.BreakLease()}

#Delete blobs in a specified container.
$blobs| Remove-AzureStorageBlob

Remove-AzureStorageContainer -Container $ContainerName -Context $Context

If you want to break a seal on a blob you can use the How to break the locked lease of blob storage in Microsoft Azure (PowerShell)

like image 140
Matija Grcic Avatar answered Nov 14 '22 19:11

Matija Grcic


$key = (Get-AzureRmStorageAccountKey -ResourceGroupName $selectedStorageAccount.ResourceGroupName -name $selectedStorageAccount.StorageAccountName -ErrorAction Stop)[0].value 
        $storageContext = New-AzureStorageContext -StorageAccountName $selectedStorageAccount.StorageAccountName -StorageAccountKey $key -ErrorAction Stop 
        $storageContainer = Get-AzureStorageContainer -Context $storageContext -Name $ContainerName -ErrorAction Stop 
        $blob = Get-AzureStorageBlob -Context $storageContext -Container  $ContainerName -Blob $BlobName -ErrorAction Stop          
        $leaseStatus = $blob.ICloudBlob.Properties.LeaseStatus; 
        If($leaseStatus -eq "Locked") 
        { 
             $blob.ICloudBlob.BreakLease() 
             Write-Host "Successfully broken lease on '$BlobName' blob." 
        } 
        Else 
        { 
            #$blob.ICloudBlob.AcquireLease($null, $null, $null, $null, $null) 
            Write-Host "The '$BlobName' blob's lease status is unlocked." 
        } 

If you want to a script for ARM resources you can use the How to break the locked lease of blob storage by ARM in Microsoft Azure(PowerShell)

like image 26
frank tan Avatar answered Nov 14 '22 19:11

frank tan


The lease is likely from something like a VM, or something else using the Blog Storage. As a result manually releasing the lease could cause problems.

With that said, the following PowerShell command should do the trick:

Get-AzureRmStorageAccount -Name "STORAGE_ACCOUNT_NAME" | Get-AzureStorageBlob -name "CONTAINER_NAME").ICloudBlob.BreakLease()

If its a VM, you should see the following post on removing the disk: Cannot delete blob: There is currently a lease on the blob and no lease ID was specified in the request

However, if you simply want to replace the drive used by every machine that uses the given blob, stopping the VM, releasing the lease, uploading a new image, and starting the VM appears to work.

like image 32
Doug Avatar answered Nov 14 '22 19:11

Doug