-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdelete_all_table_items.py
More file actions
executable file
·87 lines (66 loc) · 2.57 KB
/
delete_all_table_items.py
File metadata and controls
executable file
·87 lines (66 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python
import sys
import boto3
from botocore.exceptions import ClientError
# Needed for when the script is run in Lambda where modules are in scripts subdirectory
try:
import fire
except ImportError:
fire = None
def _handle_table_access_error(e, table_name):
error_code = e.response["Error"]["Code"]
if error_code == "ResourceNotFoundException":
print(f"Error: Table '{table_name}' does not exist")
elif error_code == "AccessDeniedException":
print(f"Error: No permission to access table '{table_name}'")
else:
print(f"Error accessing table: {e}")
sys.exit(1)
def _scan_and_delete_batch(table, scan_kwargs, deleted_count):
try:
response = table.scan(**scan_kwargs)
except ClientError as e:
if e.response["Error"]["Code"] == "ProvisionedThroughputExceededException":
print(f"\nWarning: Throttled at {deleted_count} items. Retrying...")
return scan_kwargs.get("ExclusiveStartKey"), deleted_count, True
raise
with table.batch_writer() as batch:
for item in response["Items"]:
batch.delete_item(Key=item)
deleted_count += 1
if deleted_count % 100 == 0:
print(f"Deleted {deleted_count} items...", end="\r")
return response.get("LastEvaluatedKey"), deleted_count, False
def delete_all_table_items(table_name):
dynamodb = boto3.resource("dynamodb")
table = dynamodb.Table(table_name)
try:
key_names = [key["AttributeName"] for key in table.key_schema]
except ClientError as e:
_handle_table_access_error(e, table_name)
scan_kwargs = {"ProjectionExpression": ",".join(key_names)}
deleted_count = 0
try:
while True:
last_key, deleted_count, was_throttled = _scan_and_delete_batch(
table, scan_kwargs, deleted_count
)
if was_throttled:
scan_kwargs.pop("ExclusiveStartKey", None)
if last_key:
scan_kwargs["ExclusiveStartKey"] = last_key
continue
if not last_key:
break
scan_kwargs["ExclusiveStartKey"] = last_key
except Exception as e:
print(f"\nError during deletion: {e}")
print(f"Successfully deleted {deleted_count} items before error")
sys.exit(1)
print(f"\n✓ Cleared {deleted_count} items from {table_name}")
return deleted_count
if __name__ == "__main__":
if fire is None:
print("Error: fire module not available")
sys.exit(1)
fire.Fire(delete_all_table_items)