Hope someone can help me further.
Im trying to delete files after file array which are older than 14 days.
so far now, the script deletes all files in sharename and not just the ones in filearray older than 14 days.
any help is very appriciated.
$accountName = "accountname"
$accountKey = "key"
$shareName = "share"
$filelist = az storage file list --exclude-dir -s $sharename --account-name $accountName --account-key $accountKey
$fileArray = $filelist | ConvertFrom-Json
foreach ($file in $fileArray | Where-Object {$_.properties.lastModified.DateTime -lt ((Get-Date).AddDays(-14))})
{
$removefile = $file.name
if ($removefile -ne $null)
{
Write-Host "Removing file $removefile"
az storage file delete --share-name $shareName --account-Name $accountName --account-Key $accountKey -p $removefile
}
2
Answers
Make sure your
$sharename
and$shareName
are consistent to delete the files which are older than 14 days.In my environment, I have one file which is older than 14 days.
Portal:
Now using the below modified script to delete the files which are older than 14 days.
Script:
Output:
I checked in the portal the file was deleted.
Portal:
Update:
If you need to list the files from particular directory you can use the below command.
Command:
There is a feature called lifecycle policy for Blob Storage which enables automated cleanup of blobs created or modified more than a given number of days ago. This is not currently available for File Storage but is on the roadmap, according to this feedback item. You may consider retiring this script if this feature is made available, although there is currently no ETA for this.