How to Delete All Files in an Azure File Storage Folder

How do I delete all files in an Azure File Storage folder?

ListFilesAndDirectories can return both files and directories so you get a base class for those two. Then you can check which if the types it is and cast. Note you'll want to track any sub-directories so you can recursively delete the files in those.

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
}
}
}

Delete all files in Azure Storage on a schedule

If you are looking for a simple solution you can use Azure logic app. List the items, loop through and delete one by one. Something like this:
enter image description here

Update:
Now that you are using File share you need to do it like this:
What is happening here is we are checking if we have folder then do a loop over that folder.
enter image description here

How to delete all files in root of File system with Delete Activity of Data Factory?

In source settings, select the File path type as Wildcard file path and provide the Wildcard file name as ‘*.json’ to delete all the files of type JSON.

Sample Image

Download and delete file from Azure file shares

Simply add await file.DeleteAsync to delete the file after you have read the stream.

Something like:

static void Main(string[] args)
{
// Get a connection string to our Azure Storage account.
string connectionString = ConfigurationManager.AppSettings.Get("StorageConnectionString");

// Get a reference to a share named "sample-share"
ShareClient share = new ShareClient(connectionString, ConfigurationManager.AppSettings.Get("ShareNamed"));

// Get a reference to a directory named "sample-dir"
ShareDirectoryClient dir = share.GetDirectoryClient(ConfigurationManager.AppSettings.Get("SourceDirectory"));

foreach (ShareFileItem item in dir.GetFilesAndDirectories())
{
Console.WriteLine(item.Name);
// Get a reference to a file named "sample-file" in directory "sample-dir"
ShareFileClient file = dir.GetFileClient(item.Name);

// Download the file
ShareFileDownloadInfo download = file.Download();

using (FileStream stream = File.Open(ConfigurationManager.AppSettings.Get("DestinationDirectory") + item.Name, FileMode.Append))
{
download.Content.CopyTo(stream);
stream.FlushAsync();
stream.Close();
}
await file.DeleteAsync();
}

Console.ReadLine();
}

Deleting all blobs in a directory with Azure.Storage.Blobs

You can use the GetBlobsByHierarchy method to list blobs in your directory, and then delete them.

Please refer to my code:

            BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
var resultSegment = containerClient.GetBlobsByHierarchy(BlobTraits.None, BlobStates.None, "/", "frankfolder/").AsPages(default, 100);

foreach (Azure.Page<BlobHierarchyItem> blobPage in resultSegment)
{
foreach (BlobHierarchyItem blobhierarchyItem in blobPage.Values)
{
Console.WriteLine("Blob name: {0}", blobhierarchyItem.Blob.Name);
containerClient.DeleteBlob(blobhierarchyItem.Blob.Name);
}
}

How to delete a folder from an Azure Fileshare using cli

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 :

Sample Image

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>

Sample Image

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:

Sample Image

Sample Image

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 Topics



Leave a reply



Submit