How to break the locked lease of blob storage in Microsoft Azure (PowerShell) 

 Introduction

This PowerShell script sample shows how to break the locked lease of blob storage in Microsoft Azure.

Scenarios

Microsoft Azure provides functionality to acquire lock on blobs to avoid concurrent writes to blobs.  Sometimes, if the backup fails due to prolonged or sustained network connectivity failure, the backup process may not be able gain access to the blob and the blob may remain orphaned. This means that the blob cannot be written to or deleted until the lease is released.  In this case, you might want to break the lease on a blob. This script can help you.

Script

Step 1: From the Start Screen or the Start Menu, search for Windows Azure PowerShell. Right-click the Windows Azure PowerShell entry and select Run as Administrator.

Note: If Windows Azure PowerShell is not installed, see Getting Started with Windows Azure PowerShell Cmdlets for installation and configuration information.

Step 2: If you want to get a list of all cmdlet help topics, type the command Get-Help C:\Script\ BreakBlobLease.ps1-Full to display the entire help file for this function, such as the syntax, parameters, or examples. This is shown in the following figure.
 

Example

Example 1: Type C:\Script\BreakBlobLease.ps1 -StorageAccountName "storageaccount" -ContainerName "vhd" -BlobName "3lfvawwo.p4j201403200630470102.vhd" command in the Windows PowerShell Console.

This example shows how to break lease on specified blob.

Here are some code snippets for your reference.

PowerShell
Edit|Remove
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."}

Prerequisite

Windows PowerShell 3.0
Windows Azure PowerShell