0

I am trying to delete multiple items from my DynamoDB database using a lambda (Serverless Framework) API. I have been over the code many times, but I cannot figure out how to pass multiple items to the api for deletion.

@app.route("/deletefromdb", methods=['GET', 'POST'])
def delete():
    payload = {'id': 'id_content'}
    urlShopItemsDelete = 'https://api.us-east-1.amazonaws.com/dev/todos/'
    headers = {'content-type': 'application/json'}
    res = requests.delete(
        url=urlShopItemsDelete, data=json.dumps(payload), headers=headers, timeout=15, verify=True)
    return jsonify({"success": True}), 202

API code...

import os
import json
import boto3
dynamodb = boto3.resource('dynamodb')


def delete(event, context):
    table = dynamodb.Table(os.environ['DYNAMODB_TABLE'])
    data = json.loads(event['body'])
    table.delete_item(
        Key={
            'id': data['id']
        }
    )

    response = {
        "statusCode": 200
    }

    return response

I expect for the item to be deleted by ID and then have the API return a 200 successful response...

  • Possible duplicate of [Deleting multiple records in dynamo db](https://stackoverflow.com/questions/46335395/deleting-multiple-records-in-dynamo-db) – Serhii Shliakhov Jan 16 '19 at 06:33

1 Answers1

0

Strictly speaking, you can't delete multiple items at once.

To delete an item you should provide a table name and primary key of the item. But you can execute multiple delete requests in batch

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

Serhii Shliakhov
  • 2,631
  • 3
  • 19
  • 37
  • Thank you, I appreciate the help, but how do I handle the batch request with multiple deletes on the API side? Because I can send the request, but I don't know how to handle it in my API... –  Jan 17 '19 at 00:19
  • Not sure I understand what you try to do. Maybe this question can be helpful https://stackoverflow.com/questions/21863326/delete-multiple-records-using-rest – Serhii Shliakhov Jan 17 '19 at 09:36