Rod
Rod

Reputation: 81

Azure - Blob Storage - Index Tags - How do I remove them

I have an Azure Storage Account setup and store pdf files in a blob container. Here is the config. Azure blob storage config

I'm using a c# solution to insert the files into blob storage. I have a few million pdf files in blob storage. Initially I was storing Index Tags against each blob so that I could search for them quickly. I have since moved this metadata into a Sql Server database and no longer have need for Index Tags.

I tried to remove the Index Tags with various code samples I found online including the following code:

static async Task Main(string[] args)
{
    BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
    BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);

    await foreach (BlobItem blobItem in containerClient.GetBlobsAsync(states: BlobStates.All))
    {
        BlobClient blobClient = containerClient.GetBlobClient(blobItem.Name);
        await blobClient.SetTagsAsync(new Dictionary<string, string>());
        Console.WriteLine($"Removed tags from blob: {blobItem.Name}");
    }

    Console.WriteLine("All index tags removed.");
}

The Tags look like they are no longer attached to the blobs but I am still being billed for Index Tags (which is pricey for the volume of files I have).

Does anyone know why? Have I missed something in the Tag removal process?

Cheers, Rod

Upvotes: 0

Views: 364

Answers (1)

Venkatesan
Venkatesan

Reputation: 10515

The Tags look like they are no longer attached to the blobs but I am still being billed for Index Tags (which is pricey for the volume of files I have).

Does anyone know why? Have I missed something in the Tag removal process?

The above code with blobClient.SetTagsAsync(tags) is correct for removing the all-index tag from the list of blobs using C#.

If you need to check whether the blob index tags are removed or not, you can use the below code.

Code:

string connectionString = "xxxx";
string containerName = "xxxxx";
BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);

await foreach (BlobItem blobItem in containerClient.GetBlobsAsync(states: BlobStates.All))
{
    BlobClient blobClient = containerClient.GetBlobClient(blobItem.Name);
    try
    {
        var tags = await blobClient.GetTagsAsync();
        if (tags.Value.Tags.Count > 0)
        {
            Console.WriteLine($"Blob '{blobItem.Name}' has the following tags:");
            foreach (var tag in tags.Value.Tags)
            {
                Console.WriteLine($"- {tag.Key}: {tag.Value}");
            }
        }
        else
        {
            Console.WriteLine($"Blob '{blobItem.Name}' has no tags.");
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Error checking tags for blob '{blobItem.Name}': {ex.Message}");
    }
}
Console.WriteLine("Tag check completed.");

Output:

Blob 'data.zip' has no tags.
Blob 'example.csv' has no tags.
Blob 'sample3.csv' has no tags.
Tag check completed.

enter image description here

According to this MS-Document.

  • Azure Blob Storage charges for the storage of index tags on a monthly basis. The cost depends on the average number of index tags stored per day throughout the billing cycle.
  • So, if you remove the index tags from your blobs, it may take some time for the billing to reflect the changes you made.

Reference: Set Blob Tags (REST API) - Azure Storage | Microsoft Learn

Upvotes: 0

Related Questions