How to delete old WADMetrics tables automatically from Tables storage account

Annabathina Venkateswarlu 20 Reputation points
2024-07-09T12:50:37.18+00:00

WADMetrics.JPG

I wanted to delete the Diagnostic logs(WADMetric) tables stored in the Azure table storage account. Please give me the suitable way to delete table automatically because we have hundreds of table entries. If I will processed from manual way then it will take weeks to delete tables from storage account and I need to perform same operation on over multiple storage account(more than 15).

Azure Table Storage
Azure Table Storage
An Azure service that stores structured NoSQL data in the cloud.
171 questions
Azure Storage Accounts
Azure Storage Accounts
Globally unique resources that provide access to data management services and serve as the parent namespace for the services.
3,246 questions
Azure
Azure
A cloud computing platform and infrastructure for building, deploying and managing applications and services through a worldwide network of Microsoft-managed datacenters.
1,013 questions
0 comments No comments
{count} votes

Accepted answer
  1. Sumarigo-MSFT 47,106 Reputation points Microsoft Employee
    2024-07-09T14:02:03.6466667+00:00

    @Annabathina Venkateswarlu Welcome to Microsoft Q&A Forum, Thank you for posting your query here!

    To delete Diagnostic logs (WADMetric) tables stored in an Azure Table Storage account automatically, you can use Azure Functions with a Timer Trigger.

    Here are the high-level steps to implement this solution:

    1. Create an Azure Function with a Timer Trigger that runs on a schedule (e.g., daily, weekly, etc.).
    2. Use the Azure Storage SDK to connect to the Azure Table Storage account and retrieve a list of tables.
    3. Loop through the list of tables and delete the tables that match the naming pattern for the Diagnostic logs (WADMetric) tables.
    4. Schedule the Azure Function to run on a regular basis to automatically delete the tables.

    Here is some sample code that demonstrates how to delete tables in an Azure Table Storage account using the Azure Storage SDK:

    using Microsoft.WindowsAzure.Storage;
    using Microsoft.WindowsAzure.Storage.Table;
    
    public static async Task Run(TimerInfo myTimer, ILogger log)
    {
        // Retrieve the connection string for the storage account
        string storageConnectionString = Environment.GetEnvironmentVariable("AzureWebJobsStorage");
    
        // Create a CloudStorageAccount object from the connection string
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(storageConnectionString);
    
        // Create a CloudTableClient object from the storage account
        CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
    
        // Retrieve a list of tables in the storage account
        TableContinuationToken continuationToken = null;
        do
        {
            var tableResult = await tableClient.ListTablesSegmentedAsync("WADMetricsPT1MP10DV2S", continuationToken);
            continuationToken = tableResult.ContinuationToken;
            foreach (var table in tableResult.Results)
            {
                // Delete the table if it matches the naming pattern for the Diagnostic logs (WADMetric) tables
                if (table.Name.StartsWith("WADMetrics"))
                {
                    await table.DeleteIfExistsAsync();
                    log.LogInformation($"Deleted table {table.Name}");
                }
            }
        } while (continuationToken != null);
    }
    
    

    In this example, the code retrieves a list of tables in the storage account and loops through the list to delete any tables that match the naming pattern for the Diagnostic logs (WADMetric) tables. The code logs a message for each table that is deleted.

    You can modify this code to match the naming pattern for your Diagnostic logs (WADMetric) tables and schedule the Azure Function to run on a regular basis

    Query the Tables: Use a script to query the tables that need to be deleted based on a certain condition, such as the date or name pattern.

    1. Batch Deletion: Utilize Azure Storage PowerShell cmdlets to perform batch deletions. You can use the Remove-AzStorageTable cmdlet to remove tables from your storage account.
    2. Automation: To handle multiple storage accounts, you can create a script that iterates over each storage account and performs the deletion process.
    3. Scheduling: Use Azure Automation or a similar service to schedule the PowerShell script to run at regular intervals, ensuring that the tables are deleted automatically without manual intervention.

    There is a similar discussion thread on the Stack Overflow forum. Please refer to the suggestions mentioned in that thread, which should provide some insights into your query.

    AzureDiagnosticsCleanup :A utility to clean up Azure Diagnostics logs tables in an Azure Storage Account. Over time the diagnostics tables can grow to a large size, and they are not automatically trimmed.
    Managing Azure Table Storage: Delete Table Entities using Logic APPS

    Please let us know if you have any further queries. I’m happy to assist you further.    


    Please do not forget to "Accept the answer” and “up-vote” wherever the information provided helps you, this can be beneficial to other community members.

    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  3. Annabathina Venkateswarlu 20 Reputation points
    2024-07-09T15:56:40.7133333+00:00

    Thank you so much for quick reply.

    I will try to implement and get back if any further help required.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.