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
|
2 Answers
Reset to default 0Az 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
版权声明:本文标题:azure - Az Storage file delete - x days old - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736302723a1931636.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$sharename
and$shareName
are consistent (case sensitivity matters in PowerShell) in thefilelist
. – Venkatesan Commented Nov 25, 2024 at 3:37