Click here to Skip to main content
15,867,141 members
Articles / Hosted Services / Azure
Tip/Trick

Azure Table: Clearing Out A Partition (Tip)

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
4 Aug 2016CPOL 9.1K   1
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)


Written By
Software Developer
Ireland Ireland
C# / SQL Server developer
Microsoft MVP (Azure) 2017
Microsoft MVP (Visual Basic) 2006, 2007

Comments and Discussions

 
QuestionVariable naming... Pin
Duncan Edwards Jones22-Aug-16 22:37
professionalDuncan Edwards Jones22-Aug-16 22:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.