2
votes

I have fields below in dynamo dB table

event_on -- string type

user_id -- number type

event name -- string type

Since this table may have multiple records for user_id and event_on is the single field which can be unique so I made it primary key and user_id as sort key

Now I want to delete the all records of a user, so My code is

response = dynamodb.delete_item(
            TableName=events,
            Key={
                "user_id": {"N": str(userId)}
            })

It throwing error

Exception occured An error occurred (ValidationException) when calling the DeleteItem operation: The provided key element does not match the schema

also is there anyway to delete with range

Can someone suggest me what should I have do with dynamodb table structure to make this code work

Thanks,

3

3 Answers

4
votes

It sounds like you've modeled your data using a composite primary key, which means you have both a partition key and a sort key. Here's an example of what that looks like with some sample data.

enter image description here

In DynamoDB, the most efficient way to access items (aka "rows" in RDBMS language) is by specifying either the full primary key (getItem) or the partition key (query). If you want to search by any other attribute, you'll need to use the scan operation. Be very careful with scan, since it can be a costly way (both in performance and money) to access your data.

When it comes to deletion, you have a few options.

  1. deleteItem - Deletes a single item in a table by primary key.
  2. batchWriteItem - The BatchWriteItem operation puts or deletes multiple items in one or more tables. A single call to BatchWriteItem can write up to 16 MB of data, which can comprise as many as 25 put or delete requests
  3. TimeToLive - You can utilize DynamoDBs Time To Live (TTL) feature to delete items you no longer need. Keep in mind that TTL only marks your items for deletion and actual deletion could take up to 48 hours.

In order to effectively use any of these options, you'll first need to identify which items you want to delete. Because you want to fetch using the value of the sort key alone, you have two options;

  1. Use scan to find the items of interest. This is not ideal but is an option if you cannot change your data model.
  2. Create a global secondary index (GSI) that swaps your partition key and sort key values. This pattern is called an inverted index. This would allow you to identify all items with a given user_id.

If you choose option 2, your data would look like this

enter image description here

This would allow you to fetch all item for a given user, which you could then delete using one of the methods I outlined above.

1
votes

As you can see here, delete_item needs the primary key and not the sort key. You would have to do a full scan, and delete everything that contains the given sort key.

0
votes

If you are created a DynamoDB table by the Primary key and sort key, you should provide both values to remove items from that table. If the sort key was not added to the primary key on the table creation process, the record can be removed by the Primary key.

How I solved it.

Actually, I tried to not add the sort key when created the table. And I'm using indexes for sorting and getting items.