2

Using node.js i try to delete and create dynamoDB table again. I need to delete all records from table and put new, so i think is good solution to just delete and recreate whole table. I try with this code

dynamo.deleteTable({
        TableName: tableName
    }, function(err, data){
        if (err) {
            console.log(err);
        }
        else {
            dynamo.createTable({
                TableName: tableName,
                KeySchema: [{
                    AttributeName: "id",
                    KeyType: "HASH"
                }],
                AttributeDefinitions: [{
                    AttributeName: "id",
                    AttributeType: "S"
                }],
                ProvisionedThroughput: {
                    ReadCapacityUnits: 10,
                    WriteCapacityUnits: 10
                }
            }, function(err){
                if (err) {
                    console.log(err);
                }
                else {
                    // putNewData(data, callback);
                }
            })
        }
    });

And i get error ResourceInUseException: Table already exists:

1
  • How is dynamo defined in your example? I've mostly been using DocumentClient but it doesn't have a deleteTable option. Commented Sep 6, 2023 at 22:12

2 Answers 2

3

You can use the SDK's tableNotExists waiter to ensure a table has been fully deleted before calling createTable.

Sign up to request clarification or add additional context in comments.

1 Comment

Nice solution, tnx
2

It takes some time for DynamoDB to actually remove a table, so you need to wait until the table is removed.

If you get an error ResourceInUseException, it means that the table you are trying to re-create is not deleted yet. So you need to wait for some short time and retry again.

If you are not changing what key do you have in your table and if you don't create new local secondary indexes you can use UpdateTable method instead. Here are some restrictions though:

You can only perform one of the following operations at once:

Modify the provisioned throughput settings of the table.

Enable or disable Streams on the table.

Remove a global secondary index from the table.

Create a new global secondary index on the table. Once the index begins

backfilling, you can use UpdateTable to perform other operations.

4 Comments

hmm i have thinking that callback will help me for that. How i to know when table is deleted, is that some event or just to set some timeout and hope is delete done?
I'd suggest to retry the createTable API call using exponential backoff. Doc: docs.aws.amazon.com/general/latest/gr/api-retries.html
Unfortunately there is no event when a table is deleted, and there is no set timeout that will take to remove a table. The callback is called when DynamoDB returned a reply, which can either a success or an error (if table is not deleted yet)
One other option is to use UpdateTable method instead of DeleteTable/CreateTable. I've updated the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.