Unable to delete a directory in Azure
Asked Answered
M

5

7

I have a fileshare in Azure, which contains folders inside, which in turn has many folders inside. I am trying to delete a folder manually by right clicking on the folder, which has so many files inside and it says

Failed to delete directory. Error: The specified directory is not empty.

How the directory can be deleted? There are thousands of files in the directory to be deleted and each and every file cannot be deleted manually in order to delete the directory

Machutte answered 14/9, 2020 at 15:14 Comment(1)
If you don't like to use code to delete non-empty folder, then you can use the tool Azure Storage Explorer or AzCopy. See answer below for more details.Panelboard
P
8

Update:

you can use Azure Storage Explorer(Please refer to this article about how to install it and use it.), then nav to your fileshare -> right click the folder -> select delete. This can delete a non-empty folder.

or you can use AzCopy(see here for more details about this tool) with azcopy remove command and --recursive parameter.


Original:

It's not possible to delete a non-empty folder in azure file share, you should first remove all the files inside it.

Please consider writing some code for the purpose. And there is an article which uses powershell to delete a non-empty folder. Here is the powershell code used in this article(you can also find the source code in github here):

function RemoveFileDir ([Microsoft.Azure.Storage.File.CloudFileDirectory] $dir, [Microsoft.Azure.Commands.Common.Authentication.Abstractions.IStorageContext] $ctx)
{   
    $filelist = Get-AzStorageFile -Directory $dir
    
    foreach ($f in $filelist)
    {   
        if ($f.GetType().Name -eq "CloudFileDirectory")
        {
            RemoveFileDir $f $ctx #Calling the same unction again. This is recursion.
        }
        else
        {
            Remove-AzStorageFile -File $f           
        }
    }
    Remove-AzStorageDirectory -Directory $dir
    
} 


#define varibales
$StorageAccountName = "Your Storage account name" 
$StorageAccountKey = "Your storage account primary key"
$AzShare = "your azure file share name"
$AzDirectory = "LatestPublish - your directory name under which you want to delete everything; including this directry"
 
 

#create primary region storage context
$ctx = New-AzStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey
$ctx.ToString()

#Check for Share Existence
$S = Get-AzStorageShare -Context $ctx -ErrorAction SilentlyContinue|Where-Object {$_.Name -eq $AzShare}

# Check for directory
$d = Get-AzStorageFile -Share $S -ErrorAction SilentlyContinue|select Name

if ($d.Name -notcontains $AzDirectory)
{
    # directory is not present; no action to be performed
    
}
else
{    
    $dir = Get-AzStorageFile -Share $s -Path $AzDirectory    
    RemoveFileDir $dir $ctx    
}
Panelboard answered 15/9, 2020 at 1:2 Comment(2)
Out of all the answers given, i have tried the Azcopy command to delete the non-empty folder and it works absolutely fine.Machutte
To @Ivan Yang point Azure Storage is the best solution if you like GUI or you could run az copy command yourself ``` export AZCOPY_CRED_TYPE=Anonymous; azcopy remove "https://${STORAGE-ACCOUNT-NAME}.file.core.windows.net/${DATA-STORAGE-NAME}/com/moc/${FOLDERNAME}" --from-to=FileTrash --recursive --log-level=INFO; unset AZCOPY_CRED_TYPE; ```Bassarisk
L
0

Just remove it from your machine to which it's connected.

Remove-Item -Recurse -Force "your directory"
Landin answered 14/9, 2020 at 16:1 Comment(0)
E
0
  1. Connect to your Azure container with a Virtual Machine (if file share, then go to fileshare > connect > and follow the commands to paste in your virtual machine - to connect to file share).
  2. Connect to your container in the virtual machine command interface (cd <location of your container).
  3. Delete the folder (rm -rf ).
Enid answered 5/11, 2021 at 0:55 Comment(0)
G
0

You have to map that Azure fileshare on the machine from where you have the access to it and then go to that particular directory and delete the folders inside without emptying them.

After that directory can be easily deleted.

Gesticulative answered 17/2, 2023 at 11:22 Comment(0)
B
-1

Try using the Azure CLI

export CONN_STRING="<YOUR-CONNECTION-STRING>"

az storage share delete \
   --connection-string $CONN_STRING \
   --name filesharename
Bogor answered 11/3, 2021 at 21:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.