删除 Amazon SNS 订阅和主题 - Amazon Simple Notification Service
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 Amazon Web Services 服务入门

删除 Amazon SNS 订阅和主题

您可以从 Amazon SNS 主题中删除订阅,也可以删除整个主题。请注意,您无法删除正在等待确认的订阅。三天后,Amazon SNS 会自动删除未确认的订阅。

要使用 Amazon Web Services Management Console 删除 Amazon SNS 订阅和主题

要使用 Amazon Web Services Management Console 删除订阅

  1. 登录 Amazon SNS 控制台

  2. 在左侧导航窗格中,选择订阅

  3. Subscriptions(订阅)页面上,选择状态Confirmed(已确认)的订阅,然后选择 Delete(删除)。

  4. Delete subscription(删除订阅)对话框中,选择 Delete(删除)。

    控制台删除订阅。

删除主题时,Amazon SNS 会删除与主题关联的订阅。

要使用 Amazon Web Services Management Console 删除主题

  1. 登录 Amazon SNS 控制台

  2. 在左侧导航窗格中,选择主题

  3. Topics(主题)页面上,选择一个主题,然后选择 Delete(删除)。

  4. Delete topic(删除主题)对话框中,输入 delete me,然后选择 Delete(删除)。

    控制台将删除主题。

要使用 Amazon 开发工具包删除订阅和主题

要使用 Amazon 开发工具包,您必须使用您的凭证对其进行配置。有关更多信息,请参阅 Amazon 开发工具包和工具参考指南中的共享配置和凭证文件

以下代码示例显示如何删除 Amazon SNS 主题以及该主题的所有订阅。

.NET
Amazon SDK for .NET

/// <summary> /// This example deletes an existing Amazon Simple Notification Service /// (Amazon SNS) topic. The example was created using the AWS SDK for .NET /// version 3.7 and .NET Core 5.0. /// </summary> public class DeleteSNSTopic { public static async Task Main() { string topicArn = "arn:aws:sns:us-east-2:704825161248:ExampleSNSTopic"; IAmazonSimpleNotificationService client = new AmazonSimpleNotificationServiceClient(); var response = await client.DeleteTopicAsync(topicArn); } }
  • GitHub 中查找说明和更多代码。

  • 有关 API 详细信息,请参阅 Amazon SDK for .NET API 参考中的 DeleteTopic

C++
SDK for C++

Aws::SDKOptions options; Aws::InitAPI(options); { Aws::String topic_arn = argv[1]; Aws::SNS::SNSClient sns; Aws::SNS::Model::DeleteTopicRequest dt_req; dt_req.SetTopicArn(topic_arn); auto dt_out = sns.DeleteTopic(dt_req); if (dt_out.IsSuccess()) { std::cout << "Successfully deleted topic " << topic_arn << std::endl; } else { std::cout << "Error deleting topic " << topic_arn << ":" << dt_out.GetError().GetMessage() << std::endl; } } Aws::ShutdownAPI(options);
  • GitHub 中查找说明和更多代码。

  • 有关 API 详细信息,请参阅 Amazon SDK for C++ API 参考中的 DeleteTopic

Java
SDK for Java 2.x

public static void deleteSNSTopic(SnsClient snsClient, String topicArn ) { try { DeleteTopicRequest request = DeleteTopicRequest.builder() .topicArn(topicArn) .build(); DeleteTopicResponse result = snsClient.deleteTopic(request); System.out.println("\n\nStatus was " + result.sdkHttpResponse().statusCode()); } catch (SnsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
  • GitHub 中查找说明和更多代码。

  • 有关 API 详细信息,请参阅 Amazon SDK for Java 2.x API 参考中的 DeleteTopic

JavaScript
SDK for JavaScript V3

在单独的模块中创建客户端并将其导出。

import { SNSClient } from "@aws-sdk/client-sns"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create SNS service object. const snsClient = new SNSClient({ region: REGION }); export { snsClient };

导入开发工具包和客户端模块,然后调用 API。

// Load the AWS SDK for Node.js // Import required AWS SDK clients and commands for Node.js import {DeleteTopicCommand } from "@aws-sdk/client-sns"; import {snsClient } from "./libs/snsClient.js"; // Set the parameters const params = { TopicArn: "TOPIC_ARN" }; //TOPIC_ARN const run = async () => { try { const data = await snsClient.send(new DeleteTopicCommand(params)); console.log("Success.", data); return data; // For unit tests. } catch (err) { console.log("Error", err.stack); } }; run();
Kotlin
SDK for Kotlin
注意

这是适用于预览版中功能的预发行文档。本文档随时可能更改。

suspend fun deleteSNSTopic(topicArnVal: String) { val request = DeleteTopicRequest { topicArn = topicArnVal } SnsClient { region = "us-east-1" }.use { snsClient -> snsClient.deleteTopic(request) println("$topicArnVal was successfully deleted.") } }
  • GitHub 中查找说明和更多代码。

  • 有关 API 详细信息,请参阅 Amazon SDK for Kotlin API 参考中的 DeleteTopic

PHP
SDK for PHP

require 'vendor/autoload.php'; use Aws\Sns\SnsClient; use Aws\Exception\AwsException; /** * Deletes a SNS topic and all its subscriptions. * * This code expects that you have AWS credentials set up per: * https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials.html */ $SnSclient = new SnsClient([ 'profile' => 'default', 'region' => 'us-east-1', 'version' => '2010-03-31' ]); $topic = 'arn:aws:sns:us-east-1:111122223333:MyTopic'; try { $result = $SnSclient->deleteTopic([ 'TopicArn' => $topic, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }
  • GitHub 中查找说明和更多代码。

  • 有关 API 详细信息,请参阅 Amazon SDK for PHP API 参考中的 DeleteTopic

Python
SDK for Python (Boto3)

class SnsWrapper: """Encapsulates Amazon SNS topic and subscription functions.""" def __init__(self, sns_resource): """ :param sns_resource: A Boto3 Amazon SNS resource. """ self.sns_resource = sns_resource def delete_topic(topic): """ Deletes a topic. All subscriptions to the topic are also deleted. """ try: topic.delete() logger.info("Deleted topic %s.", topic.arn) except ClientError: logger.exception("Couldn't delete topic %s.", topic.arn) raise
  • GitHub 中查找说明和更多代码。

  • 有关 API 详细信息,请参阅 Amazon SDK for Python (Boto3) API 参考中的 DeleteTopic