3

I've been attempting to delete an item from a table in DynamoDB through java code, but every attempt I've made results in the same error:

com.amazonaws.AmazonServiceException: The provided key element does not match the schema (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException;

My current attempt is very simple and looks like this:

final DynamoDB dynamoDB = new DynamoDB(new  AmazonDynamoDBClient(credentials));  

Table table =dynamoDB.getTable(tableName);    

DeleteItemSpec itemSpec = new  DeleteItemSpec().withPrimaryKey("cognitoId", cognitoId);  
table.deleteItem(itemSpec);

tablename is simply the table name, the credentials have been verified to be correct, and cognitoId is the actual ID of the item I'm trying to delete. The table in question has cognitoId as the primary key and I don't understand why the deletion isn't matching the schema. The table also has a sort key, or range key (I'm not sure what it is because the documentation is quite vague). I've been referring to the documentation here: http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/WorkingWithTables.html#WorkingWithTables.primary.key

2
  • This delete item using primary key should help you solve. Commented Jan 15, 2016 at 21:21
  • Try using deleteItem(String hashKeyName, Object hashKeyValue) Commented Jan 15, 2016 at 21:24

2 Answers 2

4

Did you have a sort key while creating a table? If so, then you have to specify the sort key too as you have a composite key on the table. Having a sort key means that you could have multiple records with the same primary key, however the sort key must be unique

http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/WorkingWithTables.html#WorkingWithTables.primary.key

The sort key may also be referred to as range or range key in the AWS Dynamo DB documentation and the console.

So your delete item would be like

DeleteItemSpec itemSpec = new DeleteItemSpec().withPrimaryKey("cognitoId", "my_id", "sortKeyField", "sort_key_id");
DeleteItemOutcome outcome = table.deleteItem(itemSpec);
Sign up to request clarification or add additional context in comments.

2 Comments

What do you mean by "composite key"?
In DynamoDB, a primary key can either be a single-attribute partition key or a composite partition-sort key
1

FYI this also happens if you provide the wrong key name, e.g.:

DeleteItemSpec itemSpec = new DeleteItemSpec().withPrimaryKey("#NOPENOTMYKEYLOL", "my_id", "sortKeyField", "sort_key_id");
DeleteItemOutcome outcome = table.deleteItem(itemSpec);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.