DynamoDB .NET - 从 table 中删除所有项目

DynamoDB .NET - Delete all items from a table

我正在学习如何使用 DynamoDB for .net,我有疑问,是否有从现有 table 中删除所有项目的正确方法?我的意思是,我不想要删除 table,只需将其清空即可。 我读过有关批处理的内容,但它们对我帮助不大。

我有这个

private string DeleteAllFromTable()
    {
        string result = string.Empty;

        try
        {

            var request = new BatchWriteItemRequest
            {
                RequestItems = new Dictionary<string, List<WriteRequest>> 
                { 
                    {
                        this.Tablename, new List<WriteRequest>
                        {
                            new WriteRequest
                            {
                                DeleteRequest = new DeleteRequest
                                {
                                    Key = new Dictionary<string,AttributeValue>()
                                    {
                                        { "Id", new AttributeValue { S = "a" } }
                                    }
                                }
                            }
                        }
                    }
                }
            };

            var response = this.DynamoDBClient.BatchWriteItem(request);

        }
        catch (AmazonDynamoDBException e)
        {

        }


        return result;
    }

当然,那只会删除与值 "a" 匹配的 id。

谢谢。

我建议您只删除 table 并重新创建它。来自 DynamoDB Guidelines for Working with Tables documentation:

...

Deleting an entire table is significantly more efficient than removing items one-by-one, which essentially doubles the write throughput as you do as many delete operations as put operations.

删除 table 并使用索引重新创建它可能会导致应用程序故障,因为 drop/create table 通常会花费很多时间,这会导致 Resource in Deleting State Error