Delete certain rows from Azure Table Storage

Andrea Previtali 106 Reputation points
2023-07-26T15:48:13.6766667+00:00

Hi all,

I have an Azure Table Storege and I store there messages sent to my IoT Hub via MQTT.

I use this table to implement something like a telemetry system but is not important.

As you imagine my table is gowing faster since many devices are sending their data to this table.

Does exist a way to realize something like a job that periodically runs and delete all the records in the table except the most recent for each device:

I try to provide an example, imagine this scenario:

Table before cleaning:

ID Time Information
Device_3 2023-07-26 02:00:00 PM Temperature: 31.0°C
Device_2 2023-07-26 02:13:00 PM Temperature: 33.4°C
Device_2 2023-07-26 02:12:00 PM Temperature: 32.9°C
Device_1 2023-07-26 02:00:00 PM Temperature: 31.4°C
Device_1 2023-07-26 02:01:00 PM Temperature: 31.7°C

Table after cleaning:

Device_3 2023-07-26 02:00:00 PM Temperature: 31.0°C
Device_2 2023-07-26 02:12:00 PM Temperature: 32.9°C
Device_1 2023-07-26 02:01:00 PM Temperature: 31.7°C

Find a way to do this every n days/hours should be perfect, alternatively what query could I activate on my table to select all but the most recent element of each device in order to delete them?

Azure Table Storage
Azure Table Storage
An Azure service that stores structured NoSQL data in the cloud.
170 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,149 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Sander van de Velde | MVP 32,641 Reputation points MVP
    2023-07-26T16:40:04.5833333+00:00

    Hello @Andrea Previtali ,

    the direct answer is to execute deletion logic using eg. a timer-triggered Azure Function or an Automation Runbook:

    var query = new TableQuery<MyEntity>().Where(TableQuery.GenerateFilterCondition("MyField", QueryComparisons.Equal, "Foo")); 
    var rows = groupsTable.ExecuteQuery(query); 
    foreach (var row in rows) 
    {     
          var tableOperation = TableOperation.Delete(row);     
          table.Execute(tableOperation); 
    }
    

    The longer answer is to reconsider Azure Table storage.

    Check out other database options like Azure Data Explorer (It sets a retention time by default. There is a free tier), giving you more flexibility.


    If the response helped, do "Accept Answer". If it doesn't work, please let us know the progress. All community members with similar issues will benefit by doing so. Your contribution is highly appreciated.


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.