Looking for an example of how to Delete an item into your DynamoDB Table using Python Boto3? This is the article for you.
Note that the full code is available at the bottom of this article.
Before you start, remember that you need proper IAM Permissions in order to interact with DynamoDB. For deleting, you require the dynamodb:DeleteItem
permision. Make sure that the user or role you are using has this permission. If you don’t know how to create an IAM user/role with a policy, check out my YouTube tutorial here.
Check out other Python DynamoDB Based Operation Articles here:
First we need to import boto3
, which is the Python SDK that allows us to interact with DynamoDB APIs.
import boto3
Next we need to get a reference to the DynamoDB resource using the below code snippet.
Note that we are using the DynamoDB resource and not the client object. The resource object offers higher level APIs which make it easier to access It is also the more modern way of interacting with Dynamo. However, resource may not always be completely up to date and you can alternatively use the client. The client offers more verbose lower level APIs. I personally suggest you use the resource object for all your interactions with Dynamo.
dynamodb = boto3.resource('dynamodb')
Next up we need to get a reference to our DynamoDB table using the following lines. Note that I am using a Lambda function (an AWS service) to interact with Dynamo. But you certainly don’t need to.
def lambda_handler(event, context):
table = dynamodb.Table('Countries')
Now we’re finally ready to perform our Delete Item operation.
The exact syntax for deleting an item depends on if you are using a SortKey/RangeKey in addition to your classic Partition Key. If you’re just using a partition key, you can use the code snippet below. However if you are using a Sort Key, you MUST also provide the sort key value in addition to the partition key value. This is because when using a PK + SK combination, multiple records can share the same PK. Therefore, in order to uniquely identify the record you want to delete, you must supply the sort key value as well.
If you’re confused between partition keys and sort keys in DynamoDB, check out this article where I compare the difference between the two.
Now to perform the DeleteItem operation, we use the following code:
response = table.delete_item(
Key={
'CountryName': 'Canada'
}
)
Note that if you run this code when a record does not exist, Dynamo will still return a success. Keep this in mind when you’re implementing your business logic. If you need to be certain that your item exists prior to attempting to delete it, you can use the get_item
or query
operation. Check out this article on querying DynamoDB with boto3.
After running this code, you can check your DynamoDB table. The record should now be removed.
Before you’re done, you’ll want to examine the HTTP status code in the response object to ensure the operation was successful. We’re looking for a 200 OK status code. You can check for it by using the following code:
status_code = response['ResponseMetadata']['HTTPStatusCode']
You may also want to print out the response object to take a look at some of its other properties. It contains interesting things such as the number of internal retries dynamo performed (if required) and the requestId.
And here’s the full code putting all these snippets together:
import boto3
dynamodb = boto3.resource('dynamodb')
def lambda_handler(event, context):
table = dynamodb.Table('Countries')
response = table.delete_item(
Key={
'CountryName': 'Canada'
}
)
status_code = response['ResponseMetadata']['HTTPStatusCode']
print(status_code)
Thanks for reading and hope this helps!
You may also enjoy these articles: