Use DeleteCluster with an Amazon SDK or CLI - Amazon Redshift
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 (PDF).

Use DeleteCluster with an Amazon SDK or CLI

The following code examples show how to use DeleteCluster.

CLI
Amazon CLI

Delete a Cluster with No Final Cluster SnapshotThis example deletes a cluster, forcing data deletion so no final cluster snapshot is created.Command:

aws redshift delete-cluster --cluster-identifier mycluster --skip-final-cluster-snapshot

Delete a Cluster, Allowing a Final Cluster SnapshotThis example deletes a cluster, but specifies a final cluster snapshot.Command:

aws redshift delete-cluster --cluster-identifier mycluster --final-cluster-snapshot-identifier myfinalsnapshot
  • For API details, see DeleteCluster in Amazon CLI Command Reference.

Go
SDK for Go V2
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

import ( "context" "errors" "log" "time" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/redshift" "github.com/aws/aws-sdk-go-v2/service/redshift/types" ) // RedshiftActions wraps Redshift service actions. type RedshiftActions struct { RedshiftClient *redshift.Client } // DeleteCluster deletes the given cluster. func (actor RedshiftActions) DeleteCluster(ctx context.Context, clusterId string) (bool, error) { input := redshift.DeleteClusterInput{ ClusterIdentifier: aws.String(clusterId), SkipFinalClusterSnapshot: aws.Bool(true), } _, err := actor.RedshiftClient.DeleteCluster(ctx, &input) var opErr *types.ClusterNotFoundFault if err != nil && errors.As(err, &opErr) { log.Println("Cluster was not found. Where could it be?") return false, err } else if err != nil { log.Printf("Failed to delete Redshift cluster: %v\n", err) return false, err } waiter := redshift.NewClusterDeletedWaiter(actor.RedshiftClient) err = waiter.Wait(ctx, &redshift.DescribeClustersInput{ ClusterIdentifier: aws.String(clusterId), }, 5*time.Minute) if err != nil { log.Printf("Wait time exceeded for deleting cluster, continuing: %v\n", err) } log.Printf("The cluster %s was deleted\n", clusterId) return true, nil }
  • For API details, see DeleteCluster in Amazon SDK for Go API Reference.

Java
SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

Delete the cluster.

/** * Deletes a Redshift cluster asynchronously. * * @param clusterId the identifier of the Redshift cluster to be deleted * @return a {@link CompletableFuture} that represents the asynchronous operation of deleting the Redshift cluster */ public CompletableFuture<DeleteClusterResponse> deleteRedshiftClusterAsync(String clusterId) { DeleteClusterRequest deleteClusterRequest = DeleteClusterRequest.builder() .clusterIdentifier(clusterId) .skipFinalClusterSnapshot(true) .build(); return getAsyncClient().deleteCluster(deleteClusterRequest) .whenComplete((response, exception) -> { if (exception != null) { // Handle exceptions if (exception.getCause() instanceof RedshiftException) { logger.info("Error: {}", exception.getMessage()); } else { logger.info("Unexpected error: {}", exception.getMessage()); } } else { // Handle successful response logger.info("The status is {}", response.cluster().clusterStatus()); } }); }
  • For API details, see DeleteCluster in Amazon SDK for Java 2.x API Reference.

JavaScript
SDK for JavaScript (v3)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

Create the client.

import { RedshiftClient } from "@aws-sdk/client-redshift"; // Set the AWS Region. const REGION = "REGION"; //Set the Redshift Service Object const redshiftClient = new RedshiftClient({ region: REGION }); export { redshiftClient };

Create the cluster.

// Import required AWS SDK clients and commands for Node.js import { DeleteClusterCommand } from "@aws-sdk/client-redshift"; import { redshiftClient } from "./libs/redshiftClient.js"; const params = { ClusterIdentifier: "CLUSTER_NAME", SkipFinalClusterSnapshot: false, FinalClusterSnapshotIdentifier: "CLUSTER_SNAPSHOT_ID", }; const run = async () => { try { const data = await redshiftClient.send(new DeleteClusterCommand(params)); console.log("Success, cluster deleted. ", data); return data; // For unit tests. } catch (err) { console.log("Error", err); } }; run();
  • For API details, see DeleteCluster in Amazon SDK for JavaScript API Reference.

Kotlin
SDK for Kotlin
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

Delete the cluster.

suspend fun deleteRedshiftCluster(clusterId: String?) { val request = DeleteClusterRequest { clusterIdentifier = clusterId skipFinalClusterSnapshot = true } RedshiftClient { region = "us-west-2" }.use { redshiftClient -> val response = redshiftClient.deleteCluster(request) println("The status is ${response.cluster?.clusterStatus}") } }
  • For API details, see DeleteCluster in Amazon SDK for Kotlin API reference.

Python
SDK for Python (Boto3)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

class RedshiftWrapper: """ Encapsulates Amazon Redshift cluster operations. """ def __init__(self, redshift_client): """ :param redshift_client: A Boto3 Redshift client. """ self.client = redshift_client def delete_cluster(self, cluster_identifier): """ Deletes a cluster. :param cluster_identifier: The cluster identifier. """ try: self.client.delete_cluster( ClusterIdentifier=cluster_identifier, SkipFinalClusterSnapshot=True ) except ClientError as err: logging.error( "Couldn't delete a cluster. Here's why: %s: %s", err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise

The following code instantiates the RedshiftWrapper object.

client = boto3.client("redshift") redhift_wrapper = RedshiftWrapper(client)
  • For API details, see DeleteCluster in Amazon SDK for Python (Boto3) API Reference.

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