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

Zain Ul Abiden 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.
55 questions
Azure Files
Azure Files
An Azure service that offers file shares in the cloud.
1,304 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,489 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.
3,226 questions
Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
2,926 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Bhargava-MSFT 31,116 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

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.