How to delete DynamoDB multiple key's?

Viewed 326
  • I have dynamodb table
  • I need to remove 2 keys(type, code) from all the items, and update another key into each item(msg)
  • id is the partitionkey
  • How to delete the attribute type from each item if my type is only Fail?
  • There is no condition for code it should be deleted from each item
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('test-table')
result = table.scan()
scan_response = result.copy()
while 'LastEvaluatedKey' in result:
     result = table.scan(ExclusiveStartKey=result['LastEvaluatedKey'])
     scan_response['Items'].extend(result['Items'])

for each in scan_response['Items']:
    table.update_item(
        Key={
            'id': each['id']
        },
        AttributeUpdates={
            'msg': {
                'Value': ''
            },
        }
    )
    table.update_item(
        Key={
            'id': each['id']
        },
        UpdateExpression='REMOVE type, code'
    )

Above code is working fine, I am able to delete type and code and update msg from every item

Documentation saying i cant do the operation at one shot(UpdateExpression, AttributeUpdates)

1 Answers

Change your update expression to include both setting the msg.Value property and removing type and code:

UpdateExpression='REMOVE type, code SET msg.Value = ""'
Related