Search code examples
aws-lambdaamazon-dynamodbaws-sdk-nodejs

How do I delete old values from my dynamodb via lambda function? Dates are timestamped in attribute names


Data Structure

I started trying something like that to read the table values but even that doesn't work.

Code for reading the table

I need to filter by device id and exclude values that are greater than 30 days. If you could give me a tip on how to filter values by attribute name, that would be great.


Solution

  • You need to Query, not Scan. Modify the below to your needs:

      const response = await documentClient
        .query({
          TableName: "<YourTableName>",
          ExpressionAttributeNames: {
            "#pk": "<deviceId>",
            "#yr": "<date attribute name>"
          },
          ExpressionAttributeValues: {
            ":pk": "<your device id>",
            ":yr": "<date value>"
          },
          FilterExpression: "#yr = :yr",
          KeyConditionExpression: "#pk = :pk",
        })
        .promise();