Created
March 25, 2019 01:10
-
-
Save andreigec/fdeb57eb911da584ad99065602517be6 to your computer and use it in GitHub Desktop.
Clear all items from dynamodb table
This file contains 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
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