question

BalaKrishna-2429 avatar image
0 Votes"
BalaKrishna-2429 asked SAIKRISHNANS-1113 commented

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

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-storage-accountsazure-filesazure-storage-explorer
image.png (46.7 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

deherman-MSFT avatar image
0 Votes"
deherman-MSFT answered SAIKRISHNANS-1113 commented

@BalaKrishna-2429
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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

if possible can u please share a script

0 Votes 0 ·

@deherman-MSFT i tried the solution that you have mentioned in the other thread, but i think its keep on running into infinite loop and stops the execution after 3 hours. Am also checking for a way to delete the directories if they are empty.

0 Votes 0 ·
BalaKrishna-2429 avatar image
0 Votes"
BalaKrishna-2429 answered BalaKrishna-2429 commented

@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

      }
  }
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.