Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Code to delete old storage tables from Azure
using System;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
/// <summary>
/// delete old metrics table both PT1H and PT1M
/// </summary>
/// <param name="tablepPrefix">WADMetricsPT1HP10DV2S and WADMetricsPT1MP10DV2S</param>
/// <returns></returns>
private async Task DeleteOldTables(string tablepPrefix)
{
try
{
string connectionString = ConfigurationManager.ConnectionStrings["StorageAccountConnectionString"].ConnectionString;
CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(connectionString);
CloudTableClient cloudTableClient = cloudStorageAccount.CreateCloudTableClient();
TableContinuationToken token = null;
do
{
string currentYear = DateTime.Now.ToString("yyyy");
int currentMonth = Convert.ToInt32(DateTime.Now.ToString("MM"));
for (int i = 1; i < currentMonth - 1; i++)
{
string currentTablePrefix = tablepPrefix + currentYear + i.ToString("d2"); //convert single digit month to two digit
var allTablesResult = await cloudTableClient.ListTablesSegmentedAsync(currentTablePrefix, token);
token = allTablesResult.ContinuationToken;
Console.WriteLine("Fetched all tables to be deleted, count: " + allTablesResult.Results.Count);
foreach (CloudTable table in allTablesResult.Results)
{
Console.WriteLine("Deleting table: " + table.Name);
await table.DeleteIfExistsAsync();
}
}
} while (token != null);
Console.WriteLine("Old Tables Deleted");
}
catch (Exception ex)
{
Console.WriteLine("Exception occured while deleting tables " + 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.