skip to Main Content

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


  1. Az Storage file delete – x days old. Im trying to delete files after file array which are older than 14 days.

    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:
    enter image description here

    Now using the below modified script to delete the files which are older than 14 days.

    Script:

    $accountName = "venkat326123"
    $accountKey = "xxxxx"
    $shareName = "share1"
    
    # Retrieve the file list
    $filelist = az storage file list --exclude-dir -s $shareName --account-name $accountName --account-key $accountKey
    $fileArray = $filelist | ConvertFrom-Json
    
    # Loop through files and delete those older than 14 days
    foreach ($file in $fileArray | Where-Object { $_.properties.lastModified -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
        }
    }
    

    Output:

    Removing file example.pdf
    {
      "deleted": null
    }
    

    enter image description here

    I checked in the portal the file was deleted.

    Portal:
    enter image description here

    Update:

    If you need to list the files from particular directory you can use the below command.

    Command:

    $accountName = "venkat326123"
    $accountKey = "xxxx"
    $shareName = "share1"
    $directoryPath = "sample/demo"  # Replace this with your target directory
    
    # Retrieve the file list from the specified directory
    $filelist = az storage file list -s $shareName --account-name $accountName --account-key $accountKey --path $directoryPath
    $fileObjects = $filelist | ConvertFrom-Json
    
    # Extract and display only the file names
    $fileObjects | ForEach-Object { $_.name }
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search