Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save springfred/ac38a68ae6c20513e141c6fce4e97875 to your computer and use it in GitHub Desktop.
Save springfred/ac38a68ae6c20513e141c6fce4e97875 to your computer and use it in GitHub Desktop.

Revisions

  1. @jonarddoci jonarddoci revised this gist Feb 16, 2017. 1 changed file with 17 additions and 9 deletions.
    26 changes: 17 additions & 9 deletions dynamodb delete all items in table
    Original file line number Diff line number Diff line change
    @@ -1,22 +1,20 @@
    var hashKey = "id";
    var rangeKey = null;
    var tableName = "alert";

    var scanParams = {
    TableName: 'table_name',
    TableName: tableName,
    };

    docClient.scan(params, function(err, data) {
    docClient.scan(scanParams, function(err, data) {
    if (err) ppJson(err); // an error occurred
    else {
    data.Items.forEach(function(obj,i){
    console.log(i);
    console.log(obj);
    var params = {
    TableName: scanParams.TableName,
    Key: { // a map of attribute name to AttributeValue for all primary key attributes

    "sampleHashKey": obj.sampleHashKey,
    "sampleRangeKey": obj.sampleRangeKey
    // more attributes...

    },
    Key: buildKey(obj),
    ReturnValues: 'NONE', // optional (NONE | ALL_OLD)
    ReturnConsumedCapacity: 'NONE', // optional (NONE | TOTAL | INDEXES)
    ReturnItemCollectionMetrics: 'NONE', // optional (NONE | SIZE)
    @@ -31,3 +29,13 @@ docClient.scan(params, function(err, data) {
    }
    });


    function buildKey(obj){
    var key = {};
    key[hashKey] = obj[hashKey]
    if(rangeKey){
    key[rangeKey] = obj[rangeKey];
    }

    return key;
    }
  2. @jonarddoci jonarddoci revised this gist Sep 2, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions dynamodb delete all items in table
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    var params = {
    var scanParams = {
    TableName: 'table_name',
    };

    @@ -9,7 +9,7 @@ docClient.scan(params, function(err, data) {
    console.log(i);
    console.log(obj);
    var params = {
    TableName: 'chatUserMapping',
    TableName: scanParams.TableName,
    Key: { // a map of attribute name to AttributeValue for all primary key attributes

    "sampleHashKey": obj.sampleHashKey,
  3. @jonarddoci jonarddoci created this gist Aug 27, 2016.
    33 changes: 33 additions & 0 deletions dynamodb delete all items in table
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    var params = {
    TableName: 'table_name',
    };

    docClient.scan(params, function(err, data) {
    if (err) ppJson(err); // an error occurred
    else {
    data.Items.forEach(function(obj,i){
    console.log(i);
    console.log(obj);
    var params = {
    TableName: 'chatUserMapping',
    Key: { // a map of attribute name to AttributeValue for all primary key attributes

    "sampleHashKey": obj.sampleHashKey,
    "sampleRangeKey": obj.sampleRangeKey
    // more attributes...

    },
    ReturnValues: 'NONE', // optional (NONE | ALL_OLD)
    ReturnConsumedCapacity: 'NONE', // optional (NONE | TOTAL | INDEXES)
    ReturnItemCollectionMetrics: 'NONE', // optional (NONE | SIZE)
    };

    docClient.delete(params, function(err, data) {
    if (err) ppJson(err); // an error occurred
    else ppJson(data); // successful response
    });

    });
    }
    });

    19 changes: 19 additions & 0 deletions dynamodb delete all tables
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    dynamodb.listTables().eachPage(function(err, data) {

    if (err) {
    ppJson(err); // an error occurred
    } else if (data) {
    console.log(data);
    data["TableNames"].forEach(function(tablename, i) {
    console.log(tablename);
    var params = {
    TableName: tablename,
    };
    dynamodb.deleteTable(params, function(err, data) {
    if (err) ppJson(err); // an error occurred
    else ppJson(data); // successful response
    });
    })

    }
    });