Azure Table Storage

Akhil Reddy 6 Reputation points
2022-03-19T13:54:03.14+00:00

I'm looking for a solution to delete old data in Azure table storage across multiple storage accounts and multiple subscriptions at a time based on last accessed time. Is there any way to apply lifecycle management for Azure table storage or is there any PowerShell script to run to delete tables across multiple storage accounts and multiple subscriptions ?

Azure Table Storage
Azure Table Storage
An Azure service that stores structured NoSQL data in the cloud.
156 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.
2,711 questions
0 comments No comments
{count} vote

1 answer

Sort by: Most helpful
  1. shiva patpi 13,141 Reputation points Microsoft Employee
    2022-03-20T05:43:55.747+00:00

    Hello @Akhil Reddy ,
    Do you want to delete the rows from the particular table or do you want to delete the whole table ?
    Is that like - you want to go through each row and based upon the record modified date - you want to delete it ?
    Or If you have multiple tables , based upon the last accessed/modified date of table - you want to delete the whole table ?

    There are couple of options mentioned at https://stackoverflow.com/questions/38588896/deleting-the-entries-in-azure-table-storage-that-are-certain-days-old-automation

    You can implement those options using Azure Functions/ WebJobs or using Powershell & C#.net.
    Sample PowerShell code: https://github.com/chriseyre2000/Powershell/tree/master/Azure2

    Basically every Azure Table Data will have a DateEventTime which indicates when that particular record was inserted. Based upon that record insertion date - some solutions have mentioned in those above articles. It's very important in-order to delete the data from Azure Tables to consider the combination of Partition & Row key or else the performance of the automation might get degraded.

    using Microsoft.WindowsAzure.Storage.Table;
    using Azure.Data.Tables;
    public static void DisplayDataFromEachTableCompareDateTimeStamp()

        {  
    
            try  
    
            {  
    
                StorageCredentials creds = new StorageCredentials("storageaccountname", "key");  
    
                CloudStorageAccount account = new CloudStorageAccount(creds, useHttps: true);  
    
                CloudTableClient client = account.CreateCloudTableClient();  
    
                var tables = client.ListTables();  
                foreach (var table in tables)  
                {  
                    Console.WriteLine("Displaying data from " +table.Name);  
                    CloudTable cloudtable = client.GetTableReference(table.Name);  
                    TableQuery tableQuery = new TableQuery();  
                    foreach (var result in table.ExecuteQuery(tableQuery))  
                    {  
                        var tblData = cloudtable.Execute(TableOperation.Retrieve(result.PartitionKey, result.RowKey));  
                        DateTimeOffset dateInserted = ((Microsoft.WindowsAzure.Storage.Table.DynamicTableEntity)tblData.Result).Timestamp;  
                        TimeSpan time = DateTime.Now.Subtract(dateInserted.DateTime);  
                        Console.WriteLine(dateInserted); ////compare the logic with the number of days and call the function cloudtable.DeleteIfExists()  
    
                          
                    }  
    
                }  
    
    
            }  
            catch (Exception ex)  
            {  
                Console.WriteLine(ex);  
            }  
        }  
    
    
     public static void RemoveEntityByPartitionAndRowKey()  
        {  
            try  
            {  
                string connectionString = "DefaultEndpointsProtocol=https;AccountName=accoutname;AccountKey=key;EndpointSuffix=core.windows.net";  
                //Connection String & Table Name  
                TableClient tableClient = new TableClient(connectionString, "WADMetricsPT1HP10DV2S20220223");  
                //delete using partition key & row key  
                tableClient.DeleteEntity(":005F:005FVM:005FOR:005FVMSS:005FRESOURCE:005FID:005F:005F", "2517565535999999999__:002Fbuiltin:002Fdisk:002Fwritebytespersecond");  
    
                  
            }  
            catch (Exception ex)  
            {  
                Console.WriteLine(ex.ToString());  
                throw;  
            }  
        }  
    

    There is also a tool called AzureTablePurge mentioned at https://brentonwebster.com/blog/deleting-stuff-from-an-azure-table-part-1
    That will help out in deleting the data from a given table based upon Partition & Row Key.

    Please note: You might have to tweak the code for Multiple Storage accounts accordingly. Multiple Subscriptions does not matter as the storage account names are unique.

    Kindly let us know if that helps!