Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete all entities with a timestamp more than 1 day old from Azure Storage Table?

Azure storage tables all have a timestamp column. Based on documentation here the listed way to delete from a storage table is to select an entity then delete it.

Does anyone know how to delete any entity from a storage table based on a datetime comparison on the timestamp value using code?

EDIT:

Based on the advice given I wrote the following code. However, it throws a Bad Request exception on my table.ExecuteQuery(rangeQuery) call. Any advice?

    StorageCredentials creds = new StorageCredentials(logAccountName, logAccountKey);
    CloudStorageAccount account = new CloudStorageAccount(creds, useHttps: true);

    CloudTableClient client = account.CreateCloudTableClient();

    CloudTable table = client.GetTableReference(LogTable);

    TableQuery<CloudQuerySummary> rangeQuery = new TableQuery<CloudQuerySummary>()
        .Where(TableQuery.GenerateFilterCondition("Timestamp", QueryComparisons.LessThan
        , DateTime.Now.AddHours(- DateTime.Now.Hour).ToString()));


    TableOperation deleteOperation;
    // Loop through the results, displaying information about the entity.
    foreach (CloudQuerySummary entity in table.ExecuteQuery(rangeQuery))
    {
        deleteOperation = TableOperation.Delete(entity);

        table.Execute(deleteOperation);
    }

EDIT 2

Here is the final working code for anyone who chooses to copy/reference it.

public void DeleteLogsNotFromToday()
{
    StorageCredentials creds = new StorageCredentials(logAccountName, logAccountKey);
    CloudStorageAccount account = new CloudStorageAccount(creds, useHttps: true);

    CloudTableClient client = account.CreateCloudTableClient();

    CloudTable table = client.GetTableReference(LogTable);

    TableQuery<CloudQuerySummary> rangeQuery = new TableQuery<CloudQuerySummary>()
        .Where(TableQuery.GenerateFilterConditionForDate("Timestamp", QueryComparisons.LessThan
        , DateTime.Now.AddHours(-DateTime.Now.Hour)));

    try
    {

        TableOperation deleteOperation;
        // Loop through the results, displaying information about the entity.
        foreach (CloudQuerySummary entity in table.ExecuteQuery(rangeQuery))
        {
            deleteOperation = TableOperation.Delete(entity);

            table.Execute(deleteOperation);
        }
    }
    catch (Exception ex)
    {
        throw;
    }

}
like image 819
Adam Heeg Avatar asked Sep 01 '15 15:09

Adam Heeg


People also ask

How do you clear a table storage in Azure?

If it is important for you to keep using the table, the only other option is to delete entities. For faster deletes, you can look at deleting entities using Entity Batch Transactions . But for deleting entities, you would need to first fetch the entities.

What is RowKey in Azure table storage?

The row key is a unique identifier for an entity within a given partition. Together the PartitionKey and RowKey uniquely identify every entity within a table. The row key is a string value that may be up to 1 KiB in size. You must include the RowKey property in every insert, update, and delete operation.

How do you delete a column in Azure table storage?

you can do the same by using azure storage explorer. You can export the table of choice, delete the property in the CSV file and import it back in new table. drop the existing table and rename the new table to the existing one.


1 Answers

You will have to do a partition scan to do that, as entities are only indexed on their PartitionKey and RowKey.

In the tutorial link you posted, look at the section Retrieve a range of entities in a partition. Once you get the entities you want to delete, you will then execute a table operation to delete them.

If you don't want to delete them one by one, you can create a batch delete operation (provided all entities to delete have the same partition key). The link above also instructs how to construct a batch operation.

Alternatively, if you do not want to do a table scan, you should store the date reference (for instance, storing the date in milliseconds as the RowKey) and then use that to filter the entities you need to delete based on a date-time comparison (something similar to THIS)

UPDATE: I think the problem is in this line: DateTime.Now.AddHours(- DateTime.Now.Hour).ToString()

As from the documentation:

The Timestamp property is a DateTime value that is maintained on the server side to record the time an entity was last modified

You are trying to compare a DateTime property with a String. I'm no C# expert, but that does not look to me as a valid comparison.

like image 80
Luis Delgado Avatar answered Oct 30 '22 04:10

Luis Delgado