0

I am trying to delete file from specific folder like from full or diff in blob container but unable to do. Container_name and then there are two folders full and diff and I want to delete file from full only. Please help.

$context = New-AzureStorageContext -StorageAccountName "storage_name" -StorageAccountKey "key"


$blobs= Get-AzureStorageBlob -Container "container_name" -blob *DIFF*.bak -Context $context
foreach ($blob in $blobs)
{
$modifieddate = $blob.LastModified
Write-Host $modifieddate

if ($modifieddate -ne $null) 
    {
        $howold = ([DateTime]::Now - [DateTime]$modifieddate.LocalDateTime) 

        if ($howold.TotalDays -ge 5)
            {
                Remove-AzureStorageBlob -Blob $blob.Name -Container "container_name" -Context $context
                Write-Host $blob.Name
            }
    }

 }
codewario
  • 19,553
  • 20
  • 90
  • 159
Parth Mehta
  • 11
  • 2
  • 4
  • Possible duplicate of [How to delete a folder within an Azure blob container](https://stackoverflow.com/questions/34727829/how-to-delete-a-folder-within-an-azure-blob-container) – codewario Aug 29 '19 at 14:32

1 Answers1

1

If you want to use Azure PowerShell to delete blobs from one subfolder in one container, you can use the following script :

$StorageAccountKey=" "
$StorageAccountName=" "
$ContainerName=" "
$Token = $null
$Total = 0
$MaxCount=5000
$context = New-AzStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey
do
 { 
     $Blobs =  Get-AzStorageBlob -Container $ContainerName -MaxCount $MaxCount  -ContinuationToken $Token -Context $context -Prefix "your fodler name"
     if($Blobs.Length -le 0) { Break;}
     $Token = $Blobs[$blobs.Count -1].ContinuationToken;

     foreach($blob in $blobs){
        Remove-AzStorageBlob -Blob $blob.Name -Container $ContainerName -Context $context

     }


 }
 While ($Token -ne $null)  

Update

I use the latest version Azure Az module to do a test. enter image description here

Jim Xu
  • 21,610
  • 2
  • 19
  • 39
  • It is not working. Giving error as unable to recognize command for prefix – Parth Mehta Aug 30 '19 at 14:13
  • According to my test, the command "Get-AzStorageBlob" has parameter "Prefix". For more details, please refer to https://learn.microsoft.com/en-us/powershell/module/az.storage/get-azstorageblob?view=azps-2.6.0 – Jim Xu Sep 02 '19 at 02:02
  • It is working fine after replacing "AzStorage" with "AzureStorage". – Parth Mehta Sep 03 '19 at 07:50