Home > Blockchain >  How to delete a folder from an Azure Fileshare using cli
How to delete a folder from an Azure Fileshare using cli

Time:11-12

I'm trying to delete the contents of a fileshare via script in order to clean it on schedule or on demand.

az storage file delete -s $myshare -p $folderOrFileName --account-name $accountName --account-key $accountKey

Allow me to delete files but I cannot delete a folder. Searching the web I've found several disjoined solutions like using azcopy tool or PS commands but none of them worked for me so far.

Help will be appreciated.

CodePudding user response:

You have to use az storage directory delete instead of az storage file delete to delete a folder present inside the Fileshare as file delete only deletes files and not the folders (directories).

I tested the Scenario in my environment :

enter image description here

But When I directly use the below command , I get another error as the directory is not empty , its not able to delete the entire directory.

 az storage directory delete --share-name test --name ansumandirectory --account-name ansumanadls1234 --account-key <accountkey>

enter image description here

So , In order to delete the Directory ,I have to delete the files present inside it as well first . So I used the below script :

$sharename = "test"
$foldername = "ansumandirectory"
$accountname = "ansumanadls1234"
$accountkey = "accountkey"
$source= "$sharename/$foldername"
az storage file delete-batch --source $source --account-name $accountname --account-key $accountkey 
az storage directory delete --share-name $sharename --name $foldername --account-name $accountname --account-key $accountkey

Output:

enter image description here

enter image description here

Note: If Your directory is Empty then you can directly use the az storage directory delete command , it will delete the folder. But if its not empty then use the az storage file delete-batch to delete all the files with the directory delete command (as done in the above script).

  • Related