0

I am trying to delete Azure Storage directory using Powershell:-

# the Context is already set correctly, not showing it here though    
if( $myshare -eq $null )
{
        $myshare=New-AzureStorageShare -Name "share" -Context $context
}
Remove-AzureStorageDirectory -ShareName "share" -Path "mycontainer/mydir/dir1" -Context $context -Confirm:$false  

I am getting the following error:-

Remove-AzureStorageDirectory : The remote server returned an error: (404) Not Found. HTTP 
Status Code: 404 - HTTP Error Message: The specified parent path does not exist.
At C:\test.ps1:21 char:1
+ Remove-AzureStorageDirectory -ShareName "share" -Path "mycontainer/my ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Remove-AzureStorageDirectory], StorageException
    + FullyQualifiedErrorId : StorageException,Microsoft.WindowsAzure.Commands.Storage.File.Cmd 
   let.RemoveAzureStorageDirectory

This is how the folder that I am trying to delete (dir1) exists in my storage:-

mycontainer/mydir/dir1

So the path very much exists. I am not sure what it's expecting otherwise.

juvchan
  • 6,113
  • 2
  • 22
  • 35
Dhiraj
  • 3,396
  • 4
  • 41
  • 80

1 Answers1

2

The Remove-AzureStorageDirectory cmdlet with ShareName and Path parameters is able to remove specific folder in the path as expected.

The only time I am able to reproduce the exact same error message as you're having is when I purposely use the wrong parent folder, which is the root folder under the storage file share.

Ensure that your parent folder under your storage file share is indeed "mycontainer".

Note: Tested with Azure PowerShell 3.4.0 (January 2017)

Update 1:

Based on the URL (https://mystorageaccount.blob.core.windows.net/mycontainer/mydir/dir1/) provided in the comment, the user's "folder" is under Storage Blob instead of File Share, which is obviously why the cmdlet above is not working.

Update 2

The PowerShell command below will remove all the blobs under the "dir1"

Get-AzureStorageBlob -Container "mycontainer" -blob "*dir1/*" -Context $context | ForEach-Object {Remove-AzureStorageBlob -Blob $_.Name -Container "mycontainer" -Context $context}

Note: Remove your code above related to File Share as they are not relevant

juvchan
  • 6,113
  • 2
  • 22
  • 35
  • basically 'mycontainer' is my container. So I am not sure if it should be part of the path. But then there is no other way this command would know what is my container name. I tried skipping container name too at one stage , directly using purely folder paths like this mydir/dir1 but got the same error. Can you please provide your example here which you are able to run error free – Dhiraj Jan 28 '17 at 11:08
  • Are you able to post the full path hiding the sensitive info? – juvchan Jan 28 '17 at 11:11
  • https://mystorageaccount.blob.core.windows.net:443/mycontainer/mydir/dir1/ – Dhiraj Jan 28 '17 at 11:16
  • Just now when browsed to the root, I noticed something strange, there are two objects at root level. There is the container called 'mycontainer' as expected, but there is also a share called 'share'. Really I want to have nothing to do with any such share -- I only wanted to delete the dir1 directory but somehow powershell cmdlet was not allowing me without share. – Dhiraj Jan 28 '17 at 11:18
  • I can understand why it's failing. Yours is not a Azure Storage FileShare, it is Azure Blob Storage instead. The file share URL format is as below https://.file.core.windows.net/, yours is obviously for blob – juvchan Jan 28 '17 at 11:24
  • So then how do I basically delete the directory in this case? – Dhiraj Jan 28 '17 at 11:29
  • Yes, there are blobs as well as directories under dir1, and these inner directories in turn will have some blobs too. Basically I want to delete dir1 along with whatever is inside it. – Dhiraj Jan 28 '17 at 11:35
  • Updated the answer based on your comment feedback, please let me know if this works and acceptable as answer to you – juvchan Jan 28 '17 at 11:55
  • It deletes the blobs inside the directory dir1 but not dir1 itself. – Dhiraj Jan 28 '17 at 12:10
  • Yes this is the correct behaviour. In fact there is no folder in the blob storage. The "folder" is actually part of the file name of the blob(s) for easier grouping. Read more here http://stackoverflow.com/questions/26718243/how-to-create-empty-folder-in-azure-blob-storage – juvchan Jan 28 '17 at 12:15
  • After you close and reopen the container in the Storage Explorer, you will no longer see the "dir1" "folder" because it has been deleted since it's part of the blob name. – juvchan Jan 28 '17 at 12:17
  • If this answer solves your question and if you appreciate the effort and time for helping, kindly mark this as acceptable answer. – juvchan Jan 29 '17 at 12:42
  • Thanks. I understand that the folder is actually name of the file name. But then even after I delete the file using the command you gave, I am still seeing 'empty folder'. No amount of refreshing the azure explorer or even disconnecting/connecting is changing that. It's still there. – Dhiraj Jan 30 '17 at 19:15