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]"
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]*'
If you have similar files in the file share like (test.json.test1.json) ,using
az storage delete-batch with pattern filter wont be possible to exclude the deletion of a particular file.
Reference SO thread how to use pattern in the az storage blob delete-batch
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"
}
}