-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcleanup.py
73 lines (57 loc) · 2.09 KB
/
cleanup.py
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
import boto3
import os
from dotenv import load_dotenv
from pathlib import Path
# Load environment variables from .env file
env_path = Path('.') / '.env'
load_dotenv(dotenv_path=env_path)
# Check if variable is loaded
bucket_name = os.environ.get('AUDIO_BUCKET')
if not bucket_name:
raise ValueError("AUDIO_BUCKET environment variable is not set")
def cleanup_s3():
"""Delete all files from S3 bucket"""
try:
s3 = boto3.client('s3')
# List all objects in the bucket
paginator = s3.get_paginator('list_objects_v2')
deleted_count = 0
for page in paginator.paginate(Bucket=bucket_name):
if 'Contents' in page:
# Get list of objects to delete
objects = [{'Key': obj['Key']} for obj in page['Contents']]
# Delete objects in batches
s3.delete_objects(
Bucket=bucket_name,
Delete={'Objects': objects}
)
deleted_count += len(objects)
print(f"✓ Deleted {deleted_count} files from S3 bucket: {bucket_name}")
except Exception as e:
print(f"Error cleaning up S3: {str(e)}")
def cleanup_dynamodb():
"""Delete all items from DynamoDB table"""
try:
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('language_entries')
# Scan for all items
response = table.scan()
items = response.get('Items', [])
# Delete each item
with table.batch_writer() as batch:
for item in items:
batch.delete_item(
Key={
'id': item['id']
}
)
print(f"✓ Deleted {len(items)} entries from DynamoDB table: language_entries")
except Exception as e:
print(f"Error cleaning up DynamoDB: {str(e)}")
def main():
print("Starting cleanup...")
cleanup_s3()
cleanup_dynamodb()
print("Cleanup complete!")
if __name__ == "__main__":
main()