kyun
kyun

Reputation: 10294

Deleting Multiple Items DynammoDB

I have a DB table in AWS DynamoDB

+------------+----------+-------------+---------------+
| email (PK) | uid (SK) | isConnected | connectionExp |
+------------+----------+-------------+---------------+

email attribute could be duplicated.

I am trying to delete items follow connectionExp.

export const delete = (e, ctx, cb) => {
  const params = {
    TableName: "MyTable",
    /*
      I want to skip Key attribute if I can.
    */
    ConditionExpression: "isConnected == :isConnected and connectionExp <= :connectionExp",
    ExpressionAttributeValues: {
      ":isConnected": false,
      ":connectionExp": moment().format("YYYY-MM-DD HH:mm:ss")
    }
  }
  DYNAMO_DB.delete(params, (err, data) => { ... });
}

The above code doesn't work.

How can I delete my items with efficiency

Upvotes: 0

Views: 42

Answers (1)

robasaurus
robasaurus

Reputation: 1032

To get your logs, either run your lambda locally and check debug console, or if running within AWS check your cloudwatch logs. That will give you the exact error, however, you're correct in your comment that the key is a required field so your code won't work in its current state.

https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_DeleteItem.html

Can you set the TTL (time to live) attribute on your table to be your connectionExp field. Dynamo will automatically delete any records surpassing your connectionExp field. This however won't take into account your isConnected field.

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/TTL.html

You can also perform a scan on the table to find the records to delete (this is slow if your table grows).

https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Scan.html

Upvotes: 1

Related Questions