Search code examples
c#azureazure-functionsazure-table-storageazure-tablequery

How to delete Azure table entities based on range query results


Am trying to delete Azure table entities based on my range filter. All am trying here is to delete the older records.

I was able to get the correct entites based on my range search condition.

TableQuery<Tables> rangeQuery = new TableQuery<Tables>().Where(
    TableQuery.CombineFilters(
    TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "DeleteTablename"),
    TableOperators.And,
    TableQuery.GenerateFilterConditionForDate("StartedOn", QueryComparisons.LessThan, DateTime.Now.Add(new TimeSpan(0, -60, 0)))));

    foreach(Tables rows in logTable.ExecuteQuery(rangeQuery)) {
        log.Info($"{rows.PartitionKey}, {rows.RowKey}\t{rows.FinishedOn}\t{rows.Timestamp}");
    }
    //till here it works
    TableResult retrievedResult = logTable.ExecuteQuery(rangeQuery);
    Tables deleteEntity = (Tables)retrievedResult.Result;

am getting following errors

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'Microsoft.WindowsAzure.Storage.Table.TableResult' timertest

Cannot convert type 'Microsoft.WindowsAzure.Storage.Table.TableResult' to 'timertest.Tables' timertest

Notes:

  • timertest is my namespace
  • Tables is a class, that extends TableEntity class to add my custom proporties in azure table.

Any ideas to delete entities based on range ?


Solution

  • Any ideas to delete entities based on range?

    In Azure Tables to perform bulk delete, you will need to use Entity Group Transaction and add the entities that you wish to delete in a batch and then execute that batch operation. For entities to be manipulated (create, update or delete) in a batch:

    • They should have the same PartitionKey value.
    • A maximum of 100 entities can be included in a batch.
    • Maximum payload size of a batch can be 4MB.

    Please note that even if one entity fails in the group transaction, the entire transaction is rolled back.

    You can learn more about entity batch transaction here: https://learn.microsoft.com/en-us/rest/api/storageservices/performing-entity-group-transactions.