Reputation: 29976
I am trying to delete all items from an aws dynamodb table by hashkey. I see alot of discussion about it on the internet but no actual code samples and all my attempts have failed.
What am I doing wrong here or is there a better apporach altogether?
List<Document> results = null;
var client = new AmazonDynamoDBClient("myamazonkey", "myamazonsecret");
var table = Table.LoadTable(client, "mytable");
var search = table.Query(new Primitive("hashkey"), new RangeFilter());
do {
results = search.GetNextSet();
search.Matches.Clear();
foreach (var r in results)
{
client.DeleteItem(new DeleteItemRequest()
{
Key = new Key() { HashKeyElement = new AttributeValue() { S = "hashkey"}, RangeKeyElement = new AttributeValue() { N = r["range-key"].AsString() } }
});
}
} while(results.Count > 0);
Upvotes: 2
Views: 6519
Reputation: 29976
Problem solved using the AWS batch write functionality. I chop mine into batches of 25 but I think the api can take up to 100.
List<Document> results = null;
var client = new AmazonDynamoDBClient("myamazonkey", "myamazonsecret");
var table = Table.LoadTable(client, "mytable");
var batchWrite = table.CreateBatchWrite();
var batchCount = 0;
var search = table.Query(new Primitive("hashkey"), new RangeFilter());
do {
results = search.GetNextSet();
search.Matches.Clear();
foreach (var document in results)
{
batchWrite.AddItemToDelete(document);
batchCount++;
if (batchCount%25 == 0)
{
batchCount = 0;
try
{
batchWrite.Execute();
}
catch (Exception exception)
{
Console.WriteLine("Encountered an Amazon Exception {0}", exception);
}
batchWrite = table.CreateBatchWrite();
}
}
if (batchCount > 0) batchWrite.Execute();
}
} while(results.Count > 0);
Upvotes: 2