Bhavik Joshi
Bhavik Joshi

Reputation: 2677

How to delete all the Existing tables in DynamoDb?

I wants to delete all the existing tables in my database in DynamoDb?

Is there any way to do it?

Upvotes: 6

Views: 15817

Answers (7)

Siddharth Verma
Siddharth Verma

Reputation: 11

To delete all the DynamoDB tables having a specific keyword in their names, simply run the following CLI commands:

tableList=$(aws dynamodb list-tables | jq .'TableNames[]' -r | grep tableNameKeyword)
for table in $tableList; do aws dynamodb delete-table --table-name $table ; done

Upvotes: 1

Rahul
Rahul

Reputation: 13056

The new UI console (2020) now allows you to select multiple tables and delete.

Upvotes: 4

Adam Loving
Adam Loving

Reputation: 443

Javascript to delete all tables

var AWS = require("aws-sdk")

AWS.config.update({
  region: "us-west-2",
  endpoint: process.env.DYNAMODB_URL
})

var dynamodb = new AWS.DynamoDB();

dynamodb.listTables({}, function(err, data) {
  if (err) console.error(err, err.stack)
  for (tableName of data.TableNames) {
    dynamodb.deleteTable({TableName: tableName}, function(err, data) {
      if (err) console.error(err, err.stack)
      else console.log('Deleted', tableName)
    })
  }
})

Upvotes: 4

sid
sid

Reputation: 61

You can delete table using below code.

var params = {
    TableName: 'table-name',
};
dynamodb.deleteTable(params, function(err, data) {
    if (err) ppJson(err); // an error occurred
    else ppJson(data); // successful response
});

Upvotes: 6

halil
halil

Reputation: 1812

You can use aws cli to delete all tables, may be except one table.

aws dynamodb list-tables --profile your_profile | jq .'TableNames[]' -r | grep -v table_you_dont_want_to_delete | xargs -ITABLE -n 1 aws dynamodb delete-table --table-name TABLE --profile your_profile

Upvotes: 13

Madhan S
Madhan S

Reputation: 937

require 'json'
tables=JSON.parse(`aws dynamodb list-tables`)["TableNames"]

tables.each do |table|
  `aws dynamodb delete-table  --table-name #{table}`
  sleep 2 # Only 10 tables can be created, updated, or deleted simultaneously
  puts "#{table} deleted"
end

Upvotes: 1

mkobit
mkobit

Reputation: 47319

You have 2 options:

  1. Manually go into the AWS console and delete every table
  2. Programmatically by calling ListTables (multiple calls if pagination is needed), iterate through the resulting TableNames, and call DeleteTable for each one.

Upvotes: 5

Related Questions