how we can remove files in the azure fileshare for specific time span?

mianzain989 0 Reputation points
2023-01-30T09:07:52.7933333+00:00

How can we delete files in an Azure file share for a specific time span? for example, if I have one file share of 1 TB of data I want to remove files created before 2019 or 2020. Is there a command that can perform this task? Can you share it with me?

Azure Data Share
Azure Data Share
An Azure service that is used to share data from multiple sources with other organizations.
49 questions
Azure Files
Azure Files
An Azure service that offers file shares in the cloud.
1,207 questions
Azure Data Lake Storage
Azure Data Lake Storage
An Azure service that provides an enterprise-wide hyper-scale repository for big data analytic workloads and is integrated with Azure Blob Storage.
1,403 questions
Azure Storage Accounts
Azure Storage Accounts
Globally unique resources that provide access to data management services and serve as the parent namespace for the services.
2,854 questions
Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
2,570 questions
{count} votes

1 answer

Sort by: Most helpful
  1. BhargavaGunnam-MSFT 28,446 Reputation points Microsoft Employee
    2023-02-02T21:45:11.01+00:00

    Hello @Zain Ul Abideen,

    Welcome to the MS Q&A platform.

    A similar thread has been discussed here.

    You can leverage the below script to delete the files for a specific time span.

    I hope this helps. Please let me know if you have any further questions.

    connect-azaccount
    $accountName = '<accountname>';
    $accountKey = '<accountkey>';
    $myshare = '<file sharename >';
    
    
     // Delete old files
    
        $filelist = az storage file list -s $myshare --account-name $accountName --account-key $accountKey
        $fileArray = $filelist | ConvertFrom-Json
        foreach ($file in $fileArray | Where-Object {$_.properties.lastModified.DateTime -lt ((Get-Date).AddDays(-90))})
        {
            $removefile = $file.name
            if ($removefile -ne $null)
            {
                Write-Host "Removing file $removefile"
                az storage file delete -s $myshare -p $removefile
            }
        }
    
    0 comments No comments