您需要使用TableOperation.Delete
:
var storageAccount = CloudStorageAccount.DevelopmentStorageAccount;
var table = storageAccount.CreateCloudTableClient()
.GetTableReference("tempTable");
table.CreateIfNotExists();
// Add item.
table.Execute(TableOperation.Insert(new TableEntity("MyItems", "123")));
// Load items.
var items = table.ExecuteQuery(new TableQuery<TableEntity>());
foreach (var item in items)
{
Console.WriteLine(item.PartitionKey + " - " + item.RowKey);
}
// Delete item (the ETAG is required here!).
table.Execute(TableOperation.Delete(new TableEntity("MyItems", "123") { ETag = "*" }));
删除仅适用于存在的实体。即使旧客户端有ContinueOnError
选项,但它与Batch
操作不兼容(如此处所述)。
如果您不知道实体是否存在,那么获得成功批量删除的唯一方法是首先添加它们(或者如果它们已经存在则替换它们):
var ensureItemsBatch = new TableBatchOperation();
ensureItemsBatch.InsertOrReplace(new MyEntity("MyItems", "123") { Active = false });
ensureItemsBatch.InsertOrReplace(new MyEntity("MyItems", "456") { Active = false });
ensureItemsBatch.InsertOrReplace(new MyEntity("MyItems", "789") { Active = false });
table.ExecuteBatch(ensureItemsBatch);
var deleteItemsBatch = new TableBatchOperation();
deleteItemsBatch.Delete(new MyEntity("MyItems", "123") { ETag = "*" });
deleteItemsBatch.Delete(new MyEntity("MyItems", "456") { ETag = "*" });
deleteItemsBatch.Delete(new MyEntity("MyItems", "789") { ETag = "*" });
table.ExecuteBatch(deleteItemsBatch);