Category: How do I do X?
Updated

This solution is summarized from an archived support forum post. This information may have changed. If you notice an error, please let us know in Discord.

Delete Item in DynamoDB

Issue

I'm trying to delete an item from a DynamoDB table using a query but I keep getting an "unexpected error." Other queries like adding new items are working fine, so I think the database is set up correctly. I've tried different syntax but I can't seem to find the right one. Can someone please help me?

Resolution

To delete an item from a DynamoDB table in AWS, you need to use the DELETE operation and specify the primary key of the item you want to delete. Here's an example query using the AWS SDK for JavaScript:

var params = {
TableName: "blocked-numbers",
Key: {
"phoneNumber": { S: "{{Table1.selectedRow.phoneNumber}}" }
}
};

docClient.delete(params, function(err, data) {
if (err) {
console.error("Unable to delete item. Error JSON:", JSON.stringify(err, null, 2));
} else {
console.log("DeleteItem succeeded:", JSON.stringify(data, null, 2));
}
});

In this example, docClient is an instance of the AWS.DynamoDB.DocumentClient class, which provides a simplified API for interacting with DynamoDB as a document database.

The params object contains the TableName and Key of the item to be deleted. The Key is a map of attribute names to attribute values for the primary key of the item, in this case the phoneNumber attribute.

The delete function sends the request to DynamoDB and receives a response with information about the deleted item, or an error if the operation was unsuccessful. The response data can be logged or further processed as needed.