DeleteDBCluster 搭配 AWS SDK 使用 - AWS SDK 程式碼範例

文件 AWS 開發套件範例 GitHub 儲存庫中有更多可用的 AWS SDK 範例

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

DeleteDBCluster 搭配 AWS SDK 使用

下列程式碼範例示範如何使用 DeleteDBCluster

Java
SDK for Java 2.x
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/** * Deletes a DB instance asynchronously. * * @param clusterId the identifier of the cluster to delete * @return a {@link CompletableFuture} that completes when the cluster has been deleted */ public CompletableFuture<Void> deleteDBClusterAsync(String clusterId) { DeleteDbClusterRequest request = DeleteDbClusterRequest.builder() .dbClusterIdentifier(clusterId) .skipFinalSnapshot(true) .build(); return getAsyncClient().deleteDBCluster(request) .thenAccept(response -> System.out.println("🗑️ Deleting DB Cluster: " + clusterId)); }
  • 如需 API 詳細資訊,請參閱《AWS SDK for Java 2.x API 參考》中的 DeleteDBCluster

Python
SDK for Python (Boto3)
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

def delete_db_cluster(neptune_client, cluster_id: str): """ Deletes a Neptune DB cluster and throws exceptions to the caller. Args: neptune_client (boto3.client): The Neptune client object. cluster_id (str): The ID of the Neptune DB cluster to be deleted. Raises: ClientError: If the delete operation fails. """ request = { 'DBClusterIdentifier': cluster_id, 'SkipFinalSnapshot': True } try: print(f"Deleting DB Cluster: {cluster_id}") neptune_client.delete_db_cluster(**request) except ClientError as err: code = err.response["Error"]["Code"] message = err.response["Error"]["Message"] if code == "DBClusterNotFoundFault": print(f"Cluster '{cluster_id}' not found or already deleted.") elif code == "AccessDeniedException": print("Access denied. Please ensure you have the necessary permissions.") else: print(f"Couldn't delete DB cluster. {code}: {message}") raise
  • 如需 API 詳細資訊,請參閱《適用於 Python (Boto3) 的AWS SDK API 參考》中的 DeleteDBCluster