1回答
- 新しい順
- 投票が多い順
- コメントが多い順
1
You can delete an item with primary keys (I assume primary keys are 'id' and 'email' in this situation).
Here's an example:
class Example:
def __init__(self, dyn_resource):
self.dyn_resource = dyn_resource
self.table = None
def delete_item(self, id, email):
"""
Deletes an item from the table.
"""
try:
self.table.delete_item(Key={"id": id, "email": email})
except ClientError as err:
logger.error(
"Couldn't delete an item %s. Here's why: %s: %s",
title,
err.response["Error"]["Code"],
err.response["Error"]["Message"],
)
raise
I share this example to help you by referring an example for python (boto3) from AWS's document [1]. You can also refer to boto3's document 'delete_item [2]' to read more details about 'delete_item'
References:
[1] Delete an item in a DynamoDB table https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.DeleteItem.html
[2] boto3 - DynamoDB/Client/delete_item https://docs.aws.amazon.com/goto/boto3/dynamodb-2012-08-10/DeleteItem
@Myungsp_k - thanks. what about , if i create an item , with a same primary key that already exists in the table, it should override the existing item , with new values , right?
Hello clouduser, According to AWS document 'PutItem [1]', default PutItem operation when there's an already existing item is to overwrite it. But you can perform a conditional put operation [2] (add a new item if one with the specified primary key doesn't exist), or replace an existing item if it has certain attribute values.
Please refer to documents I share below.
Thanks you.
References: [1] PutItem https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_PutItem.html [2] Condition expressions - Conditional put https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.ConditionExpressions.html#Expressions.ConditionExpressions.PreventingOverwrites