Delete a DynamoDB table using an Amazon SDK - Amazon DynamoDB
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China.

Delete a DynamoDB table using an Amazon SDK

The following code examples show how to delete a DynamoDB table.

.NET
Amazon SDK for .NET
Tip

To learn how to set up and run this example, see GitHub.

public static async Task<bool> DeleteTableAsync(AmazonDynamoDBClient client, string tableName) { var request = new DeleteTableRequest { TableName = tableName, }; var response = await client.DeleteTableAsync(request); if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) { Console.WriteLine($"Table {response.TableDescription.TableName} successfully deleted."); return true; } else { Console.WriteLine("Could not delete table."); return false; } }
  • For API details, see DeleteTable in Amazon SDK for .NET API Reference.

C++
SDK for C++
Tip

To learn how to set up and run this example, see GitHub.

Aws::Client::ClientConfiguration clientConfig; if (!region.empty()) clientConfig.region = region; Aws::DynamoDB::DynamoDBClient dynamoClient(clientConfig); Aws::DynamoDB::Model::DeleteTableRequest dtr; dtr.SetTableName(table); const Aws::DynamoDB::Model::DeleteTableOutcome& result = dynamoClient.DeleteTable(dtr); if (result.IsSuccess()) { std::cout << "Your Table \"" << result.GetResult().GetTableDescription().GetTableName() << " was deleted!\n"; } else { std::cout << "Failed to delete table: " << result.GetError().GetMessage(); }
  • For API details, see DeleteTable in Amazon SDK for C++ API Reference.

Go
SDK for Go V2
Tip

To learn how to set up and run this example, see GitHub.

// TableBasics encapsulates the Amazon DynamoDB service actions used in the examples. // It contains a DynamoDB service client that is used to act on the specified table. type TableBasics struct { DynamoDbClient *dynamodb.Client TableName string } // DeleteTable deletes the DynamoDB table and all of its data. func (basics TableBasics) DeleteTable() error { _, err := basics.DynamoDbClient.DeleteTable(context.TODO(), &dynamodb.DeleteTableInput{ TableName: aws.String(basics.TableName)}) if err != nil { log.Printf("Couldn't delete table %v. Here's why: %v\n", basics.TableName, err) } return err }
  • For API details, see DeleteTable in Amazon SDK for Go API Reference.

Java
SDK for Java 2.x
Tip

To learn how to set up and run this example, see GitHub.

public static void deleteDynamoDBTable(DynamoDbClient ddb, String tableName) { DeleteTableRequest request = DeleteTableRequest.builder() .tableName(tableName) .build(); try { ddb.deleteTable(request); } catch (DynamoDbException e) { System.err.println(e.getMessage()); System.exit(1); } System.out.println(tableName +" was successfully deleted!"); }
  • For API details, see DeleteTable in Amazon SDK for Java 2.x API Reference.

JavaScript
SDK for JavaScript V3
Tip

To learn how to set up and run this example, see GitHub.

Create the client.

// Create the DynamoDB service client module using ES6 syntax. import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; // Set the AWS Region. export const REGION = "REGION"; // For example, "us-east-1". // Create an Amazon DynamoDB service client object. export const ddbClient = new DynamoDBClient({ region: REGION });

Delete the table.

// Import required AWS SDK clients and commands for Node.js import { DeleteTableCommand } from "@aws-sdk/client-dynamodb"; import { ddbClient } from "./libs/ddbClient.js"; // Set the parameters export const params = { TableName: "CUSTOMER_LIST_NEW", }; export const run = async () => { try { const data = await ddbClient.send(new DeleteTableCommand(params)); console.log("Success, table deleted", data); return data; } catch (err) { console.log("Error", err); } }; run();
  • For API details, see DeleteTable in Amazon SDK for JavaScript API Reference.

SDK for JavaScript V2
Tip

To learn how to set up and run this example, see GitHub.

// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create the DynamoDB service object var ddb = new AWS.DynamoDB({apiVersion: '2012-08-10'}); var params = { TableName: process.argv[2] }; // Call DynamoDB to delete the specified table ddb.deleteTable(params, function(err, data) { if (err && err.code === 'ResourceNotFoundException') { console.log("Error: Table not found"); } else if (err && err.code === 'ResourceInUseException') { console.log("Error: Table in use"); } else { console.log("Success", data); } });
Kotlin
SDK for Kotlin
Note

This is prerelease documentation for a feature in preview release. It is subject to change.

Tip

To learn how to set up and run this example, see GitHub.

suspend fun deleteDynamoDBTable(tableNameVal: String) { val request = DeleteTableRequest { tableName = tableNameVal } DynamoDbClient { region = "us-east-1" }.use { ddb -> ddb.deleteTable(request) println("$tableNameVal was deleted") } }
  • For API details, see DeleteTable in Amazon SDK for Kotlin API reference.

PHP
SDK for PHP
Tip

To learn how to set up and run this example, see GitHub.

public function deleteTable(string $TableName) { $this->customWaiter(function () use ($TableName) { return $this->dynamoDbClient->deleteTable([ 'TableName' => $TableName, ]); }); }
  • For API details, see DeleteTable in Amazon SDK for PHP API Reference.

Python
SDK for Python (Boto3)
Tip

To learn how to set up and run this example, see GitHub.

class Movies: """Encapsulates an Amazon DynamoDB table of movie data.""" def __init__(self, dyn_resource): """ :param dyn_resource: A Boto3 DynamoDB resource. """ self.dyn_resource = dyn_resource self.table = None def delete_table(self): """ Deletes the table. """ try: self.table.delete() self.table = None except ClientError as err: logger.error( "Couldn't delete table. Here's why: %s: %s", err.response['Error']['Code'], err.response['Error']['Message']) raise
  • For API details, see DeleteTable in Amazon SDK for Python (Boto3) API Reference.

Ruby
SDK for Ruby
Tip

To learn how to set up and run this example, see GitHub.

# Deletes the table. def delete_table @table.delete @table = nil rescue Aws::Errors::ServiceError => e puts("Couldn't delete table. Here's why:") puts("\t#{e.code}: #{e.message}") raise end
  • For API details, see DeleteTable in Amazon SDK for Ruby API Reference.

Rust
SDK for Rust
Note

This documentation is for an SDK in preview release. The SDK is subject to change and should not be used in production.

Tip

To learn how to set up and run this example, see GitHub.

async fn delete_table(client: &Client, table: &str) -> Result<(), Error> { client.delete_table().table_name(table).send().await?; println!("Deleted table"); Ok(()) }
  • For API details, see DeleteTable in Amazon SDK for Rust API reference.

For a complete list of Amazon SDK developer guides and code examples, see Using DynamoDB with an Amazon SDK. This topic also includes information about getting started and details about previous SDK versions.