Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
/// <summary>
/// code to delete data from logs table
/// </summary>
/// <param name="numberOfDays">days before which all data should be deleted</param>
/// <returns></returns>
private async Task DeleteOldData(int numberOfDays)
{
string connectionString = ConfigurationManager.ConnectionStrings["StorageAccountConnectionString"].ConnectionString;
CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(connectionString);
CloudTableClient cloudTableClient = cloudStorageAccount.CreateCloudTableClient();
CloudTable cloudTable = cloudTableClient.GetTableReference("WADPerformanceCountersTable"); //do this for all other logs table too
//https://gauravmantri.com/2012/02/17/effective-way-of-fetching-diagnostics-data-from-windows-azure-diagnostics-table-hint-use-partitionkey/
string ticks = "0" + DateTime.UtcNow.AddDays(-numberOfDays).Ticks.ToString(); //calculate number of ticks for required date for faster query
TableQuery<TableEntity> query = new TableQuery<TableEntity>().
Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.LessThanOrEqual, ticks));
Console.WriteLine("Deleting data from " + cloudTable.Name + " using query " + query.FilterString);
TableContinuationToken token = null;
try
{
do
{
Console.WriteLine("Fetching Records");
TableQuerySegment<TableEntity> resultSegment = await cloudTable.ExecuteQuerySegmentedAsync(query, token);
token = resultSegment.ContinuationToken;
Console.WriteLine("Fetched all records to be deleted, count: " + resultSegment.Results.Count);
foreach (TableEntity entity in resultSegment.Results)
{
Console.WriteLine("Deleting entry with TimeStamp: " + entity.Timestamp.ToString());
TableOperation deleteOperation = TableOperation.Delete(entity);
await cloudTable.ExecuteAsync(deleteOperation);
}
} while (token != null);
Console.WriteLine("Entities Deleted from " + cloudTable.Name);
}
catch (Exception ex)
{
Console.WriteLine("Exception occured while deleting data from table " + cloudTable.Name + " " + ex.Message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.