My Code is below is to delete contents from the table name details
Capacity
of dynamodb which is working fine 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)
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)