user14177720
user14177720

Reputation:

How to delete all the items in the dynamodb with boto3

My Code is below is to delete contents from the table name details

    import boto3
    def lambda_handler(event, context):
        try:
            table_name = 'details'
            dynamodb = boto3.resource('dynamodb')
            table = dynamodb.Table(table_name)
            scan = table.scan()
            with table.batch_writer() as batch:
                for each in scan['Items']:
                    batch.delete_item(
                        Key={
                            'id': each['id']
                        }
                    )
        except Exception as e:
           print (e)

I wrote with while loop with a Flag condition.

   import boto3
    def lambda_handler(event, context):
        try:
            flag = False
            table_name = 'details'
            dynamodb = boto3.resource('dynamodb')
            table = dynamodb.Table(table_name)
            scan = table.scan()
            while True:
                with table.batch_writer() as batch:
                    for each in scan['Items']:
                        if each is not None:
                            batch.delete_item(
                                 Key={
                                 'id': each['id']
                                 }
                             )
                         else:
                            Flag = True
        except Exception as e:
           print (e)

Upvotes: 0

Views: 11413

Answers (2)

Derek Menénedez
Derek Menénedez

Reputation: 2377

For DynamoDB if you want to delete all the items the best way it's to delete and recreate the table, because with boto3 you have a limit of 1000 elements per page.

The problem with boto3 is the expensive cost... every delete it's a write request. If you don't want pay unnecessarily (and is the best way) delete and recreate :)

By the way...

import boto3
    def lambda_handler(event, context):
        try:
            flag = False
            table_name = 'details'
            dynamodb = boto3.resource('dynamodb')
            table = dynamodb.Table(table_name)
            scan = table.scan()
            while !flag:
                with table.batch_writer() as batch:
                    for each in scan['Items']:
                        batch.delete_item(
                                 Key={
                                 'id': each['id']
                                 }
                             )
                    flag = True
        except Exception as e:
           print (e)

Upvotes: 1

Sian Lerk Lau
Sian Lerk Lau

Reputation: 173

I can't edit the accepted answer due to the edit queue being full.

Looking at the code, it only scans and deletes items once.

Here's a working code by using the LastEvaluatedKey key to determine whether a rescan is necessary. The key exists when the scan reaches the maximum dataset size limit of 1 MB.

import boto3

def lambda_handler(event, context):
     try:
          table_name = 'details'
          dynamodb = boto3.resource('dynamodb')
          table = dynamodb.Table(table_name)

          flag = True
          while flag:
               scan = table.scan()
               print(f"Deleting {scan['ScannedCount']} records...")
               flag = 'LastEvaluatedKey' in scan and scan['LastEvaluatedKey']
               with table.batch_writer() as batch:
                    for each in scan['Items']:
                         batch.delete_item(
                              Key={
                                   'id': each['id']
                              }
                         )
     except Exception as e:
          print(e)

Upvotes: 1

Related Questions