Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Hosted-services / Azure

Azure Table: Clearing Out A Partition (Tip)

5.00/5 (1 vote)
4 Aug 2016CPOL 9.5K  
How to clear out a partition in an Azure table

Introduction

An Azure table has rows made of a combined key that is made of the "partition key" and the "row key". It is usual, when dealing with Azure tables, to put alike items in the same partition. This means that there may be circumstances where you want to totally clear out a partition. However, you can only do 100 database operations in any batch - therefore this code runs batches of 99 to delete all the rows in the partition... effectively clearing out the partition.

Using the Code

This assumes you have a connection to the Azure table through a CloudTable instance called myTable and the partition key is in a string variable called myKey:

VB.NET
if myTable IsNot Nothing Then
    'A Batch Operation allows a maximum 100 entities in the batch which must 
    'share the same PartitionKey 
    Dim projectionQuery = New TableQuery(Of DynamicTableEntity)().Where_
    (TableQuery.GenerateFilterCondition("PartitionKey",
        QueryComparisons.Equal, myKey)).Select({"RowKey"}).Take(99)
        
    Dim moreBatches As Boolean = True
    While moreBatches
        Dim batchDelete = New TableBatchOperation()
        For Each e In myTable.ExecuteQuery(projectionQuery)
            batchDelete.Delete(e)
        Next
        
        moreBatches = (batchDelete.Count >= 99)
        
        If (batchDelete.Count > 0) Then
            myTable.ExecuteBatch(batchDelete)
        End If
    End While
End If

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)