How delete a file in a file share in a storage account

Juan Carlos Villalobos Moreno 0 Reputation points
2025-03-10T15:59:05.73+00:00

Hi, I have a file in a file share and that fileshare is in a storage account, i need to delete the file automatically, how do I delete it adding a task or other things that automatic delete it?

Azure Files
Azure Files
An Azure service that offers file shares in the cloud.
1,384 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Keshavulu Dasari 4,280 Reputation points Microsoft External Staff
    2025-03-10T17:25:44.4066667+00:00

    Hi Juan Carlos Villalobos Moreno,

    To automatically delete a file in an Azure file share within a storage account, you can use Azure Automation or Azure Functions. Using Azure Automation

    Create an Automation Account:

    • Go to the Azure portal.
    • Navigate to "Create a resource" and search for "Automation".
    • Create an Automation Account.

    Create a Runbook:

    • In your Automation Account, go to "Runbooks" and click "Create a runbook".
    • Choose "PowerShell" as the runbook type and give it a name.

    Add PowerShell Script:

    • Use the following script to delete files older than a specific number of days:
    param (
        [string]$storageAccountName,
        [string]$storageAccountKey,
        [string]$fileShareName,
        [int]$daysOld
    )
    
    $context = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey
    $files = Get-AzStorageFile -ShareName $fileShareName -Context $context
    
    foreach ($file in $files) {
        if ($file.LastModified.DateTime -lt (Get-Date).AddDays(-$daysOld)) {
            Remove-AzStorageFile -ShareName $fileShareName -Path $file.Name -Context $context
        }
    }
    

    Publish and Schedule the Runbook:

    • Publish the runbook.
    • Go to "Schedules" and create a new schedule to run the runbook at your desired frequency.

    Using Azure Functions

    Create an Azure Function:

    • Go to the Azure portal.
    • Navigate to "Create a resource" and search for "Function App".
    • Create a Function App.

    Create a Timer Trigger Function:

    • In your Function App, create a new function with a Timer trigger.
    • Use the following code to delete files:
    using System;
    using Microsoft.Azure.WebJobs;
    using Microsoft.Extensions.Logging;
    using Microsoft.WindowsAzure.Storage;
    using Microsoft.WindowsAzure.Storage.File;
    
    public static void Run(TimerInfo myTimer, ILogger log)
    {
        string storageAccountName = Environment.GetEnvironmentVariable("StorageAccountName");
        string storageAccountKey = Environment.GetEnvironmentVariable("StorageAccountKey");
        string fileShareName = Environment.GetEnvironmentVariable("FileShareName");
        int daysOld = int.Parse(Environment.GetEnvironmentVariable("DaysOld"));
    
        CloudStorageAccount storageAccount = new CloudStorageAccount(new StorageCredentials(storageAccountName, storageAccountKey), true);
        CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
        CloudFileShare fileShare = fileClient.GetShareReference(fileShareName);
        CloudFileDirectory rootDir = fileShare.GetRootDirectoryReference();
    
        foreach (IListFileItem item in rootDir.ListFilesAndDirectories())
        {
            if (item is CloudFile file && file.Properties.LastModified < DateTimeOffset.UtcNow.AddDays(-daysOld))
            {
                file.DeleteIfExists();
            }
        }
    }
    

    Add the necessary application settings for StorageAccountName, StorageAccountKey, FileShareName, and DaysOld.Deploy the function and configure the timer trigger to run at your desired frequency.

    If you have any other questions or are still running into more issues, let me know in the "comments" and I would be glad to assist you.


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.