Deleting Files in Azure File Share older than X Days

John Kuntz 1 Reputation point
2021-07-20T12:44:36.41+00:00

I'm currently trying to write a powershell script to delete files older than 14 days from our Azure file shares. The problem I'm running into is, the files don't return a last modified date, except in the actual UI in portal.azure.com. I have created a context using my access keys, but this still doesn't seem to work. Below is the code I'm using (sanitized for privacy) so far.

$StorageAccountKey = "\

Azure Files
Azure Files
An Azure service that offers file shares in the cloud.
1,170 questions
{count} votes

1 answer

Sort by: Most helpful
  1. deherman-MSFT 33,626 Reputation points Microsoft Employee
    2021-07-26T16:22:56.827+00:00

    @John Kuntz

    Apologies for the delayed response. For “Get-AzStorageFile”:
    • Without “-Path”: It will list file/FileDir which directly in a share/fileDir. To list recursively in a share/FileDir, you need to call it recursively.
    • With “-Path”: It will get an instance of a directory or file (with file properties like LMT) in the specified path.

    The following script will list files/FileDir recursively in a file share, and delete the files older than 14 days.

    $ctx = New-AzStorageContext -StorageAccountName $accountName -StorageAccountKey $key  
    $shareName = <shareName>  
      
    $DirIndex = 0  
    $dirsToList = New-Object System.Collections.Generic.List[System.Object]  
      
    # Get share root Dir  
    $shareroot = Get-AzStorageFile -ShareName $shareName -Path . -context $ctx   
    $dirsToList += $shareroot   
      
    # List files recursively and remove file older than 14 days   
    While ($dirsToList.Count -gt $DirIndex)  
    {  
        $dir = $dirsToList[$DirIndex]  
        $DirIndex ++  
        $fileListItems = $dir | Get-AzStorageFile  
        $dirsListOut = $fileListItems | where {$_.GetType().Name -eq "AzureStorageFileDirectory"}  
        $dirsToList += $dirsListOut  
        $files = $fileListItems | where {$_.GetType().Name -eq "AzureStorageFile"}  
      
        foreach($file in $files)  
        {  
            # Fetch Attributes of each file and output  
            $task = $file.CloudFile.FetchAttributesAsync()  
            $task.Wait()  
      
            # remove file if it's older than 14 days.  
            if ($file.CloudFile.Properties.LastModified -lt (Get-Date).AddDays(-14))  
            {  
                ## print the file LMT  
                # $file | Select @{ Name = "Uri"; Expression = { $_.CloudFile.SnapshotQualifiedUri} }, @{ Name = "LastModified"; Expression = { $_.CloudFile.Properties.LastModified } }   
      
                # remove file  
                $file | Remove-AzStorageFile  
            }  
        }  
        #Debug log  
        # Write-Host  $DirIndex $dirsToList.Length  $dir.CloudFileDirectory.SnapshotQualifiedUri.ToString()   
    }   
    

    Hope this helps! Let us know if you run into issues or have further questions.

    -------------------------------

    Please don’t forget to "Accept the answer" and “up-vote” wherever the information provided helps you, this can be beneficial to other community members.

    1 person found this answer helpful.