Skip to content

Instantly share code, notes, and snippets.

@andreigec
Created March 25, 2019 01:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreigec/fdeb57eb911da584ad99065602517be6 to your computer and use it in GitHub Desktop.
Save andreigec/fdeb57eb911da584ad99065602517be6 to your computer and use it in GitHub Desktop.
Clear all items from dynamodb table
function breakArrayIntoGroups(data, maxPerGroup) {
const groups = [];
for (let index = 0; index < data.length; index += maxPerGroup) {
groups.push(data.slice(index, index + maxPerGroup));
}
return groups;
}
var wipeTable = async tableName => {
const db = new AWS.DynamoDB({ apiVersion: "2012-10-08" });
var info = await db
.describeTable({
TableName: tableName
})
.promise();
var keyHash = info.Table.KeySchema.find(k => k.KeyType === "HASH")
.AttributeName;
console.log("key hash=", keyHash);
var scan = await db
.scan({
TableName: tableName
})
.promise();
console.log(`will delete ${scan.Items.length} items from ${tableName}`);
const datagr = breakArrayIntoGroups(scan.Items, 25);
for (let i = 0; i < datagr.length; i += 1) {
const po = [];
for (let i2 = 0; i2 < datagr[i].length; i2 += 1) {
const item = datagr[i][i2];
po.push(item);
}
await db
.batchWriteItem({
RequestItems: {
[`${tableName}`]: po.map(s => ({
DeleteRequest: {
Key: {
[`${keyHash}`]: s[`${keyHash}`]
}
}
}))
}
})
.promise();
}
console.log("cleared table");
};
await wipeTable("test");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment