Reputation: 1
I have 3 months of old data which is stored on azure storage account ? Now i want remove the data
if it >= 30days
Upvotes: 0
Views: 2252
Reputation: 10859
The following script lists files/FileDir recursively in a file share and delete the files older than 14 days.You may give the required day limit. Refered from thread here.
Refer this for the best practices before delete activity.
$ctx = New-AzStorageContext -StorageAccountName $accountName -StorageAccountKey $key
$shareName = <shareName>
$DirIndex = 0
$dirsToList = New-Object System.Collections.Generic.List[System.Object]
# Get share root Dir
$shareroot = Get-AzStorageFile -ShareName $shareName -Path . -context $ctx
$dirsToList += $shareroot
# List files recursively and remove file older than 14 days
While ($dirsToList.Count -gt $DirIndex)
{
$dir = $dirsToList[$DirIndex]
$DirIndex ++
$fileListItems = $dir | Get-AzStorageFile
$dirsListOut = $fileListItems | where {$_.GetType().Name -eq "AzureStorageFileDirectory"}
$dirsToList += $dirsListOut
$files = $fileListItems | where {$_.GetType().Name -eq "AzureStorageFile"}
foreach($file in $files)
{
# Fetch Attributes of each file and output
$task = $file.CloudFile.FetchAttributesAsync()
$task.Wait()
# remove file if it's older than 14 days.
if ($file.CloudFile.Properties.LastModified -lt (Get-Date).AddDays(-14))
{
## print the file LMT
# $file | Select @{ Name = "Uri"; Expression = { $_.CloudFile.SnapshotQualifiedUri} }, @{ Name = "LastModified"; Expression = { $_.CloudFile.Properties.LastModified } }
# remove file
$file | Remove-AzStorageFile
}
}
#Debug log
# Write-Host $DirIndex $dirsToList.Length $dir.CloudFileDirectory.SnapshotQualifiedUri.ToString()
}
(OR)
Delete activity can be configured from Azure data Factory(ADF) by following the steps below.This require linking your azure storage account with the ADF by giving account name, fileshare name and path if needed.
The Process is
:We get the metadata of the file shares in storage account selected>Loop through them>configure Delete activity for the files inside them older than 30 days(or say some x days)
@adddays(utcnow(),-30)
@activity("Get old files").output.childItems
and click on
finish. ( Here Get old files is the name of my get activity created
previously )@dataset().FileName
Other reference: clean-up-files-by-built-in-delete-activity-in-azure-data-factory/
Upvotes: 1