Use Azure Cli to Delete Old Files in Azure file share

2.3k Views Asked by At

There is an attribute for blob storage, named '--if-unmodified-since', which makes it possible to delete blobs that are older than a number of days. I don't find such an attribute for Files in Cli. Is there a solution (with cli) for it?

1

There are 1 best solutions below

0
On BEST ANSWER

No Attribute similar to --if-unmodified-since but you can leverage a logic shown below for the time being:

 // Delete old files block

    $filelist = az storage file list -s $myshare --account-name $accountName --account-key $accountKey
    $fileArray = $filelist | ConvertFrom-Json
    foreach ($file in $fileArray | Where-Object {$_.properties.lastModified.DateTime -lt ((Get-Date).AddDays(-90))})
    {
        $removefile = $file.name
        if ($removefile -ne $null)
        {
            Write-Host "Removing file $removefile"
            az storage file delete -s $myshare -p $removefile
        }
    }