3

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.

Demiurge
  • 35
  • 1
  • 4
  • https://learn.microsoft.com/en-us/cli/azure/storage/fs/directory?view=azure-cli-latest#az_storage_fs_directory_delete – Liam Nov 10 '21 at 15:31
  • If that's not correct, please be clear on what "fileshare" actually means. As their are numerous technologies that this could equate too in Azure – Liam Nov 10 '21 at 15:32
  • Delete an empty folder with https://learn.microsoft.com/en-us/cli/azure/storage/directory?view=azure-cli-latest#az_storage_directory_delete Use this command to list files in a directory and then delete each one: https://learn.microsoft.com/en-us/cli/azure/storage/file?view=azure-cli-latest#az_storage_file_list – John Hanley Nov 11 '21 at 02:47
  • Edit your question to clearly state what you are trying (one example) and the error message. Saying **none of them worked for me so far** is not a clear problem that can be solved. – John Hanley Nov 11 '21 at 02:49

1 Answers1

4

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).

Ansuman Bal
  • 9,705
  • 2
  • 10
  • 27
  • Yes. That is the solution I found. I had to do a script to recursively traverse the folder tree as only empty folders can be deleted. – Demiurge Nov 12 '21 at 07:40
  • Glad to be of Help @Demiurge :)) – Ansuman Bal Nov 12 '21 at 07:41
  • 1
    When running az storage directory delete cli command, do not add "/" at the end of the folder name, otherwise you'll be getting "InvalidUri: The request URI is invalid" error. What @AnsumanBal-MT did was right. – AlvinH Feb 25 '22 at 04:19
  • but what if the folder contains folders, the folders will not get deleted.. – Ben Mar 13 '23 at 09:43