How to delete an item from dynamodb ?

0

I am new to dynamo db. I have created a table with a primary key and some other attributes (sample below) . coming from a relational database, is it possible , to delete an item based on two attributes , such as delete item when age = 38 and email = "abc@mail.com" , how do i apply and condition when using boto3 client ?

also, if i use put_item to update an item , with the same id , that should override whatever is present in the table with the same id, right? i'm using this instead of update_item call, i simply just use put_item to update all the attributes for the same primary key.

id | age | email | ...
1 | 38   | abc@mail.com ...
.....
asked 3 months ago368 views
1 Answer
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

profile picture
EXPERT
answered 3 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions