admin管理员组

文章数量:1122832

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
}

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
}
Share Improve this question edited Nov 22, 2024 at 15:29 D. de W. asked Nov 22, 2024 at 15:28 D. de W.D. de W. 11 bronze badge 1
  • Ensure that $sharename and $shareName are consistent (case sensitivity matters in PowerShell) in the filelist. – Venkatesan Commented Nov 25, 2024 at 3:37
Add a comment  | 

2 Answers 2

Reset to default 0

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:

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
}

I checked in the portal the file was deleted.

Portal:

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 }

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.

本文标签: azureAz Storage file deletex days oldStack Overflow