Search code examples
azurerestazure-storage

Not able to delete an Azure share using AZ CLNot able to delete an Azure share using AZ CLI or REST APII or REST API


I'm not able to delete a share, having the following issues:

az storage share delete --name share1 --account-key $key --account-name $sa_name --delete-snapshots include

Error:

Unable to delete share because one or more share snapshots have active leases. Release the share snapshot leases or delete the share with the include-leased parameter for x-ms-delete-snapshots.

When using REST API to breat a lease:

body='
{
  "action": "Break",
  "leaseid": "LEASE_ID",
  "breakPeriod": null,
  "leaseDuration": null,
  "proposedLeaseId": null
}'
headers='
{
  "x-ms-lease-id": "LEASE_ID",
  "x-ms-lease-action": "break",
  "x-ms-date": "2023-10-22T23:40:54.0000000Z"
}'
uri="https://management.azure.com/subscriptions/{SubID}/resourceGroups/{RG_NAME}/providers/Microsoft.Storage/storageAccounts/{SA_NAME}/fileServices/default/shares/{SHARE_NAME}/lease?>
az rest --method post --body "$body" --headers "$headers" --uri "$uri"

Error:

Conflict({"error":{"code":"LeaseNotPresentWithLeaseOperation","message":"LeaseNotPresentWithLeaseOperationThere is currently no lease on the file share.\nRequestId:003d9a62-901a-00a8-0a12-0609c8000000\nTime:2023-10-24T00:39:16.3735661Z"}})

Any help would be appreciated. Thanks.


Solution

  • Unable to delete share because one or more share snapshots have active leases. Release the share snapshot leases or delete the share with theinclude-leased parameter for x-ms-delete-snapshots.

    The error shows in your File-share has some active snapshots or lease present in some files so that cannot be deleted.

    I agree with Daredevil's comment which Lease may be on a snapshot instead of the main share.

    In my environment, I have a file with a lease like the below:

    Portal:

    enter image description here

    You can use the below command to break the lease in Azure file share.

    Command:

    $headers = @{
        "x-ms-lease-action"= "break"
    }
    
    $uri = "https://<storage account name>.file.core.windows.net/<sharename>/<path upto filename>?comp=lease&sv=2022-11-02&ss=bfqt&srt=sco&sp=rwdlacupiytfx&se=2023-10-24T12:16:53Z&st=2023-10-24T04:16:53Z&spr=https&sig=xxx" #fileurl+sastoken
    
    Invoke-RestMethod -Method Put -Headers $headers -Uri $uri
    

    The above command will break the lease for the file.

    Output:

    enter image description here

    Now you can use the same command to delete the share it will delete now.

    Reference:

    Lease File (FileREST API) - Azure Files | Microsoft Learn