Created
April 2, 2020 09:32
-
-
Save xenon92/e3a8d54782cf8fda455a46ddb0a7a1c9 to your computer and use it in GitHub Desktop.
DynamoDB - Delete all records from a table
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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