question

AkhilReddy-6338 avatar image
0 Votes"
AkhilReddy-6338 asked SumanthMarigowda-MSFT commented

Azure Table Storage

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-storage-accountsazure-table-storage
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

shivapatpi-MSFT avatar image
0 Votes"
shivapatpi-MSFT answered

Hello @AkhilReddy-6338,
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!

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.