Skip to content

[QUERY] Is it possible to delete a Blob through a BlobItem? #20092

@WillRock19

Description

@WillRock19

Query/Question
I'm converting a logic from an old library to a new one. In here, I'm getting all blobs from a container, filtering it by the LastModified property, and then deleting it. With the old library, I could do this in a very straight foward manner:

        var storageConnectionString = ConfigurationManager.ConnectionStrings["AzureWebJobsStorage"].ConnectionString;
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(storageConnectionString);
	CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

	var containerHostArchive = blobClient.GetContainerReference("azure-jobs-host-archive");
	if (containerHostArchive.Exists())
	{
		var blobsHostArchive = containerHostArchive
			.ListBlobs()
			.OfType<CloudBlob>()
			.Where(b => b.Properties.LastModified < new DateTimeOffset(DateTime.Now.AddDays(-RETENTION_DAYS_JOBS_LOGS)))
			.ToList();

		foreach (var item in blobsHostArchive)
		{
			item.DeleteIfExists();
		}
	}

In the new library, I'm not finding a easy way to do this. I have to make two access to the storage, first one to find the BlobItems from a container, the second to get a reference to the BlobClient so I can delete it (see bellow):}

                var blobContainerClient = storageWrapper.GetContainerReference(_connectionString, containerName); //Returns a BlobContainerClient
                var dateLastWeek = DateTime.Now.AddDays(-7);

                var desiredBlobs = blobContainerClient
                    .GetBlobs()
                    .ToList()
                    .Where(x => x.Properties.LastModified < dateLastWeek)
                    .ToList();

                var blobsToDelete = desiredBlobs
                    .Select(blob => storageWrapper.GetBlobReferenceNew(_connectionString, containerName, blob.Name)) //GetReferenceNew returns a BlobClient
                    .ToList();

                blobsToDelete.ForEach(blob => blob.DeleteIfExists());

Is there a way to do this without accessing the storage twice?

Environment:

  • .Net Version: Full Framework 4.8
  • Original version: Microsoft.WindowsAzure.Storage.Blob 9.3.2.0
  • New version: Azure.Storage.Blobs v12.8.0
  • IDE and version : Visual Studio 16.8.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    ClientThis issue is related to a non-management packageService AttentionWorkflow: This issue is responsible by Azure service team.StorageStorage Service (Queues, Blobs, Files)customer-reportedIssues that are reported by GitHub users external to the Azure organization.needs-team-attentionWorkflow: This issue needs attention from Azure service team or SDK teamquestionThe issue doesn't require a change to the product in order to be resolved. Most issues start as that

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions