The script is not checking inside the directory recursively . Script only checking the current or root directory . But i want to remove the data along with folder recursively if it's older than 30 days . Here am i added the script
# List files recursively and remove file older than 30 days
$Subscription="XXXXXXXXXXXXXXXX"
$StorageAccountName="xxxxxxxxxxxxxxxxxxxxxxxxx"
$ResourceGroupName="xxxxxxx"
$fileshareName = "folder"
$StorageAccountAccessKey = Get-AzStorageAccountKey -ResourceGroupName $ResourceGroupName -AccountName $StorageAccountName | Where-Object
{$_.KeyName -eq "key1"}
$ctx = New-AzStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountAccessKey.Value
$DirIndex = 0
$dirsToList = New-Object System.Collections.Generic.List[System.Object]
$shareroot = Get-AzStorageFile -ShareName $fileshareName -context $ctx
$dirsToList += $shareroot
Write-Host "DirIndex: "$DirIndex
Write-Host "dirsToList: "$dirsToList.Count
$dir = $dirsToList[$DirIndex]
$DirIndex ++
$fileListItems = $dir | Get-AzStorageFile -ShareName $fileshareName
$dirsListOut = $fileListItems | where {$_.GetType().Name -eq "AzureStorageFileDirectory"}
$files = $fileListItems | where {$_.GetType().Name -eq "AzureStorageFile"}
foreach($file in $files)
{
Write-Host "Current fileName: "$file.Name
# Fetch Attributes of each file and output
$task = $file.CloudFile.FetchAttributesAsync()
$task.Wait()
# remove file if it's older than 30 days.
if ($file.CloudFile.Properties.LastModified -lt (Get-Date).AddDays(-30))
{
Write-Host "file :",$file.Name "is older than 35 days so removing it ..!"
Remove-AzStorageFile -ShareName $fileshareName -Path $file.Name -Context $ctx
}
}