#!/usr/bin/env node require('dotenv').config({ path: '.env.local' }); const AWS = require('aws-sdk'); // Configure AWS AWS.config.update({ region: process.env.AWS_REGION || 'eu-central-1' }); const dynamodb = new AWS.DynamoDB.DocumentClient(); const TABLE_NAME = process.env.DYNAMODB_TABLE_NAME || 'filamenteka-filaments'; async function clearTable() { console.log(`Clearing all items from ${TABLE_NAME}...`); try { // First, scan to get all items const scanParams = { TableName: TABLE_NAME, ProjectionExpression: 'id' }; const items = []; let lastEvaluatedKey = null; do { if (lastEvaluatedKey) { scanParams.ExclusiveStartKey = lastEvaluatedKey; } const result = await dynamodb.scan(scanParams).promise(); items.push(...result.Items); lastEvaluatedKey = result.LastEvaluatedKey; } while (lastEvaluatedKey); console.log(`Found ${items.length} items to delete`); // Delete in batches of 25 const chunks = []; for (let i = 0; i < items.length; i += 25) { chunks.push(items.slice(i, i + 25)); } for (const chunk of chunks) { const params = { RequestItems: { [TABLE_NAME]: chunk.map(item => ({ DeleteRequest: { Key: { id: item.id } } })) } }; await dynamodb.batchWrite(params).promise(); console.log(`Deleted ${chunk.length} items`); } console.log('Table cleared successfully!'); } catch (error) { console.error('Error clearing table:', error); process.exit(1); } } // Run if called directly if (require.main === module) { clearTable(); }