Truncate DynamoDB tables from JavaScript

The following script will truncate a DynamoDB table:

const AWS = require("aws-sdk");

AWS.config.region = "us-east-1";  

const dynamoDb = new AWS.DynamoDB();

async function truncate(tableName) {
    const rows = await dynamoDb.scan({
        TableName: tableName,
        AttributesToGet: ['id'],
    }).promise();

    console.log(`Deleting ${rows.Items.length} records`);
    rows.Items.forEach(async function(element, i) {
        await dynamoDb.deleteItem({
            TableName: tableName,
            Key: element,
        }).promise();
    });
}

await truncate("events");

Leave a Reply

Your email address will not be published. Required fields are marked *