Skip to content

Instantly share code, notes, and snippets.

@xenon92
Created April 2, 2020 09:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xenon92/e3a8d54782cf8fda455a46ddb0a7a1c9 to your computer and use it in GitHub Desktop.
Save xenon92/e3a8d54782cf8fda455a46ddb0a7a1c9 to your computer and use it in GitHub Desktop.
DynamoDB - Delete all records from a table
const AWS = require("aws-sdk"); //AWS SDK
//AWS configuraton
AWS.config.update({
region: 'us-east-2'
});
//DynamoDb client
let docClient = new AWS.DynamoDB.DocumentClient();
/**************************************************************/
var hashKey = "KEY_COLUMN_HERE";
var rangeKey = null;
var tableName = "TABLE_NAME_HERE";
var scanParams = {
TableName: tableName,
};
docClient.scan(scanParams, (error, data) => {
if (error) console.log(`Error in SCAN operation: ${error}`)
else {
data.Items.forEach((item, index) => {
console.log(`Deleting dynamodb item at index ${index} :\n ${JSON.stringify(item, null, 4)}`);
var params = {
TableName: scanParams.TableName,
Key: buildKey(item),
ReturnValues: 'NONE', // optional (NONE | ALL_OLD)
ReturnConsumedCapacity: 'NONE', // optional (NONE | TOTAL | INDEXES)
ReturnItemCollectionMetrics: 'NONE', // optional (NONE | SIZE)
};
docClient.delete(params, function (error, data) {
if (error) console.log(`Error in DELETE operation: ${error}`);
});
});
}
});
function buildKey(obj) {
var key = {};
key[hashKey] = obj[hashKey]
if (rangeKey) {
key[rangeKey] = obj[rangeKey];
}
return key;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment