How to delete all files in the Azure file storage folder?

I am trying to work on how to delete all files in a folder in an Azure file vault.

CloudFileDirectory.ListFilesAndDirectories() returns IEnumerable of IListFileItem . But that doesn’t help much because it does not have a file name property or the like.

This is what I have so far:

 var folder = root.GetDirectoryReference("myfolder"); if (folder.Exists()) { foreach (var file in folder.ListFilesAndDirectories()) { // How do I delete 'file' } } 

How can I change IListFileItem to CloudFile so that I can call myfile.Delete() ?

+7
source share
2 answers

ListFilesAndDirectories can return both files and directories, so you get the base class for the two. Then you can check which types and which types. Note that you want to track any subdirectories so that you can recursively delete files in them.

 var folder = root.GetDirectoryReference("myfolder"); if (folder.Exists()) { foreach (var item in folder.ListFilesAndDirectories()) { if (item.GetType() == typeof(CloudFile)) { CloudFile file = (CloudFile)item; // Do whatever } else if (item.GetType() == typeof(CloudFileDirectory)) { CloudFileDirectory dir = (CloudFileDirectory)item; // Do whatever } } } 
+14
source

This implementation is very easy to implement with Recursion in PowerShell. When you specify a directory [may be the root directory in your case], and then the entire contents of this directory, including all files, the subdirectories are deleted. Refer to github finished PowerShell for the same - https://github.com/kunalchandratre1/DeleteAzureFilesDirectoriesPowerShell

If you are looking for a step by step guide, follow the link below - https://sanganakauthority.blogspot.com/2019/07/delete-azure-file-storage-directory-and.html

0
source

Source: https://habr.com/ru/post/1244138/


All Articles