Last active
January 31, 2022 13:08
-
-
Save Townsheriff/46dfab76c7dff139470b9a70f1790924 to your computer and use it in GitHub Desktop.
When you want to delete all items, but not to delete 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 tableName = ''; | |
| const partitionKeyName = ''; | |
| const sortKeyName = ''; | |
| while (true) { | |
| const scanParams = { | |
| TableName: tableName, | |
| Limit: 100, | |
| }; | |
| const response = await dynamoDB.scan(scanParams).promise(); | |
| if (!response.Items!.length) { | |
| break; | |
| } | |
| const promises = response.Items!.map((item) => { | |
| const params: any = { | |
| TableName: tableName, | |
| Key: { | |
| [partitionKeyName]: item[partitionKeyName], | |
| }, | |
| }; | |
| if (sortKeyName) { | |
| params.Key[sortKeyName] = item[sortKeyName]; | |
| } | |
| console.log(params.Key[partitionKeyName]); | |
| return dynamoDB.delete(params).promise(); | |
| }); | |
| await Promise.all(promises); | |
| } |
Author
Townsheriff
commented
Mar 18, 2021
Author
const tableName = "SandboxDatabase";
const partitionKeyName = "organizationId";
const sortKeyName = "identifier";
const aws = require("aws-sdk");
const dynamoDB = new aws.DynamoDB.DocumentClient({
region: "eu-central-1",
});
(async () => {
while (true) {
const scanParams = {
TableName: tableName,
Limit: 100,
};
const response = await dynamoDB.scan(scanParams).promise();
console.log("Batched pulled", response.Items.length);
if (!response.Items!.length) {
console.log("All done");
break;
}
const promises = response.Items!.map((item: any) => {
const params: any = {
TableName: tableName,
Key: {
[partitionKeyName]: item[partitionKeyName],
},
};
if (sortKeyName) {
params.Key[sortKeyName] = item[sortKeyName];
}
return dynamoDB.delete(params).promise();
});
console.log("Starting to delete", promises.length);
await Promise.all(promises);
console.log("Batch deleted", promises.length);
}
})();
Author
const TABLE_NAME = "SandboxDatabase";
const HASH_KEY = "organizationId";
const SORT_KEY = "identifier";
const aws = require("aws-sdk");
const dynamoDB = new aws.DynamoDB.DocumentClient({
region: "eu-central-1",
});
const scanItems = async () => {
const scanParams = {
TableName: TABLE_NAME,
AttributesToGet: [SORT_KEY, HASH_KEY],
};
const response = await dynamoDB.scan(scanParams).promise();
return response.Items || [];
};
const deleteItems = async <
T extends { [HASH_KEY]: string; [SORT_KEY]: string }
>(
items: T[]
) => {
if (!items.length) {
return;
}
const promises = [];
while (items && items.length) {
const itemsInBatch = Math.min(25, items.length);
const batchItems = items.splice(0, itemsInBatch).map((item) => {
return {
DeleteRequest: {
Key: {
[HASH_KEY]: item[HASH_KEY],
[SORT_KEY]: item[SORT_KEY],
},
},
};
});
const promise = dynamoDB
.batchWrite({
RequestItems: {
[TABLE_NAME]: batchItems,
},
})
.promise();
promises.push(promise);
}
return Promise.all(promises);
};
(async () => {
while (true) {
const items = await scanItems();
console.log("Batched pulled", items.length);
if (!items.length) {
console.log("All done");
break;
}
await deleteItems(items);
console.log("Items deleted");
}
})();
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment