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
:
if myTable IsNot Nothing Then
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