DeleteDBInstance 搭配 a AWS SDK 或 CLI 使用 - AWS SDK 程式碼範例

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

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

DeleteDBInstance 搭配 a AWS SDK 或 CLI 使用

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

動作範例是大型程式的程式碼摘錄,必須在內容中執行。您可以在下列程式碼範例的內容中看到此動作:

.NET
AWS SDK for .NET
注意

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

/// <summary> /// Delete a particular DB instance. /// </summary> /// <param name="dbInstanceIdentifier">DB instance identifier.</param> /// <returns>DB instance object.</returns> public async Task<DBInstance> DeleteDBInstance(string dbInstanceIdentifier) { var response = await _amazonRDS.DeleteDBInstanceAsync( new DeleteDBInstanceRequest() { DBInstanceIdentifier = dbInstanceIdentifier, SkipFinalSnapshot = true, DeleteAutomatedBackups = true }); return response.DBInstance; }
C++
C++ 的 SDK
注意

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

Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; Aws::RDS::RDSClient client(clientConfig); Aws::RDS::Model::DeleteDBInstanceRequest request; request.SetDBInstanceIdentifier(dbInstanceIdentifier); request.SetSkipFinalSnapshot(true); request.SetDeleteAutomatedBackups(true); Aws::RDS::Model::DeleteDBInstanceOutcome outcome = client.DeleteDBInstance(request); if (outcome.IsSuccess()) { std::cout << "DB instance deletion has started." << std::endl; } else { std::cerr << "Error with RDS::DeleteDBInstance. " << outcome.GetError().GetMessage() << std::endl; result = false; }
CLI
AWS CLI

刪除資料庫執行個體

下列delete-db-instance範例會在建立名為 的最終資料庫快照後刪除指定的資料庫執行個體test-instance-final-snap

aws rds delete-db-instance \ --db-instance-identifier test-instance \ --final-db-snapshot-identifier test-instance-final-snap

輸出:

{ "DBInstance": { "DBInstanceIdentifier": "test-instance", "DBInstanceStatus": "deleting", ...some output truncated... } }
  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 DeleteDBInstance

Go
SDK for Go V2
注意

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

import ( "context" "errors" "log" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/rds" "github.com/aws/aws-sdk-go-v2/service/rds/types" ) type DbInstances struct { RdsClient *rds.Client } // DeleteInstance deletes a DB instance. func (instances *DbInstances) DeleteInstance(ctx context.Context, instanceName string) error { _, err := instances.RdsClient.DeleteDBInstance(ctx, &rds.DeleteDBInstanceInput{ DBInstanceIdentifier: aws.String(instanceName), SkipFinalSnapshot: aws.Bool(true), DeleteAutomatedBackups: aws.Bool(true), }) if err != nil { log.Printf("Couldn't delete instance %v: %v\n", instanceName, err) return err } else { return nil } }
Java
Java 2.x 的 SDK
注意

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

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.rds.RdsClient; import software.amazon.awssdk.services.rds.model.DeleteDbInstanceRequest; import software.amazon.awssdk.services.rds.model.DeleteDbInstanceResponse; import software.amazon.awssdk.services.rds.model.RdsException; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class DeleteDBInstance { public static void main(String[] args) { final String usage = """ Usage: <dbInstanceIdentifier>\s Where: dbInstanceIdentifier - The database instance identifier\s """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String dbInstanceIdentifier = args[0]; Region region = Region.US_WEST_2; RdsClient rdsClient = RdsClient.builder() .region(region) .build(); deleteDatabaseInstance(rdsClient, dbInstanceIdentifier); rdsClient.close(); } public static void deleteDatabaseInstance(RdsClient rdsClient, String dbInstanceIdentifier) { try { DeleteDbInstanceRequest deleteDbInstanceRequest = DeleteDbInstanceRequest.builder() .dbInstanceIdentifier(dbInstanceIdentifier) .deleteAutomatedBackups(true) .skipFinalSnapshot(true) .build(); DeleteDbInstanceResponse response = rdsClient.deleteDBInstance(deleteDbInstanceRequest); System.out.print("The status of the database is " + response.dbInstance().dbInstanceStatus()); } catch (RdsException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } } }
Kotlin
Kotlin 的 SDK
注意

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

suspend fun deleteDatabaseInstance(dbInstanceIdentifierVal: String?) { val deleteDbInstanceRequest = DeleteDbInstanceRequest { dbInstanceIdentifier = dbInstanceIdentifierVal deleteAutomatedBackups = true skipFinalSnapshot = true } RdsClient { region = "us-west-2" }.use { rdsClient -> val response = rdsClient.deleteDbInstance(deleteDbInstanceRequest) print("The status of the database is ${response.dbInstance?.dbInstanceStatus}") } }
PHP
適用於 PHP 的 SDK
注意

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

require __DIR__ . '/vendor/autoload.php'; use Aws\Exception\AwsException; //Create an RDSClient $rdsClient = new Aws\Rds\RdsClient([ 'region' => 'us-east-1' ]); $dbIdentifier = '<<{{db-identifier}}>>'; try { $result = $rdsClient->deleteDBInstance([ 'DBInstanceIdentifier' => $dbIdentifier, ]); var_dump($result); } catch (AwsException $e) { echo $e->getMessage(); echo "\n"; }
Python
SDK for Python (Boto3)
注意

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

class InstanceWrapper: """Encapsulates Amazon RDS DB instance actions.""" def __init__(self, rds_client): """ :param rds_client: A Boto3 Amazon RDS client. """ self.rds_client = rds_client @classmethod def from_client(cls): """ Instantiates this class from a Boto3 client. """ rds_client = boto3.client("rds") return cls(rds_client) def delete_db_instance(self, instance_id): """ Deletes a DB instance. :param instance_id: The ID of the DB instance to delete. :return: Data about the deleted DB instance. """ try: response = self.rds_client.delete_db_instance( DBInstanceIdentifier=instance_id, SkipFinalSnapshot=True, DeleteAutomatedBackups=True, ) db_inst = response["DBInstance"] except ClientError as err: logger.error( "Couldn't delete DB instance %s. Here's why: %s: %s", instance_id, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return db_inst