Last active
August 20, 2019 15:23
-
-
Save garima2510/dbc5bd1e0b8c21544acc04c811683b62 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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