How to remove azure file share old data from the azure storage account?

Bala Krishna 101 Reputation points
2021-07-27T09:55:41.1+00:00

I have 3 months of old data which is stored on azure storage account ? Now i want remove the data if it >= 30days. some thing like retention period

118282-image.png

Azure Files
Azure Files
An Azure service that offers file shares in the cloud.
1,162 questions
Azure Storage Explorer
Azure Storage Explorer
An Azure tool that is used to manage cloud storage resources on Windows, macOS, and Linux.
229 questions
Azure Storage Accounts
Azure Storage Accounts
Globally unique resources that provide access to data management services and serve as the parent namespace for the services.
2,684 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. deherman-MSFT 33,296 Reputation points Microsoft Employee
    2021-07-27T21:50:25.03+00:00

    @Bala Krishna
    Azure Files does not currently have this functionality built-in. Please comment and upvote this existing feedback item. You could create automation from the client side which deletes the files from the directory or create a script using Azure functions or PowerShell. This thread has a PowerShell script which might be helpful.

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

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

    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.


  2. Bala Krishna 101 Reputation points
    2021-08-29T23:57:43.7+00:00

    @deherman-MSFT

    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  
    
         }  
     }