Last active
August 20, 2019 15:23
-
-
Save garima2510/51cdabbba81ee67758b02c65a98ce023 to your computer and use it in GitHub Desktop.
Code to delete old storage tables from Azure
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> | |
/// 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