Delete empty folders in azure file share storage using powershell script

Tiago Matte Debastiani 1 Reputation point
2022-10-17T11:47:47.987+00:00

I'm using the script below in order to delete all the files in an Azure File Share Storage that are older than 30 days. But now there are several empty folders and subfolders in this File Share Storage, is there a way to delete these empty folders/subfolders that I can add to the script?

$ctx = New-AzStorageContext -StorageAccountName $accountName -StorageAccountKey $key  
 $shareName = 
Azure Storage
Azure Storage
Globally unique resources that provide access to data management services and serve as the parent namespace for the services.
3,529 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Vukasin Terzic 351 Reputation points MVP
    2022-10-18T03:17:03.893+00:00

    Hello @Tiago Matte Debastiani ,

    This should be easy. Removing folders by AzStorage cmdlets can be done by this cmdlet:

    Remove-AzStorageDirectory

    More info about it: https://learn.microsoft.com/en-us/powershell/module/az.storage/remove-azstoragedirectory?view=azps-9.0.0

    You already have a full list of directories in $dirsToList, so you don't need to collect this information again. The cmdlet will show an error if you try to delete a non-empty folder, but I still suggest that you verify if the folder really is empty and then run the remove command. You can also use -WhatIf until you are sure that only empty folders are removed.

    If you need more help or the working script please let us know.

    Vukasin


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.