azureazure-storageazure-clifnmatch

Azure CLI - az storage file delete-batch: pattern for all but one specific file


I'm trying to delete all files from an Azure Storage File share and exclude one specific file name. I just can't figure out the pattern.

I've tried this but the pattern doesn't match anything now.

az storage file delete-batch --source "https://myst.file.core.windows.net/myshare" --sas-token $sastoken --pattern "[!importantfile.py]"

Solution

  • You need to add the pattern parameter in single quotes as shown in the below

    az storage file delete-batch --source "https://myst.file.core.windows.net/myshare" --sas-token $sastoken --pattern '[!importantfile.py]*' 
    

    Alternatively, if want a particular file to be excluded from deletion in file share you can use the below power shell script

    connect-azaccount
    $accountName = '<accountname>';
    $accountKey = '<accountkey>';
    $myshare = '<file sharename >';
    $notremovalfile = '<file that need to be excluded from deletion>';
    
     $filelist = az storage file list -s $myshare --account-name $accountName --account-key $accountKey
    
        $fileArray = $filelist|ConvertFrom-Json 
    
    
        foreach ($file in $fileArray)
        {
           if($file.name -ne $notremovalfile)
           {
                Write-Host $file.name
                az storage file delete --account-name $accountName --account-key $accountKey -s $myshare -p $file.name
                Write-Host "deleting $file.name"
            }
              
    
        }