删除 Amazon SNS 订阅和主题
删除主题后,其关联订阅会异步删除。虽然客户仍然可以访问这些订阅,但即使您使用相同的名称重新创建主题,这些订阅也不再与该主题相关联。如果发布者尝试向已删除的主题发布消息,发布者将收到一条错误消息,指出该主题不存在。同样,任何订阅已删除主题的尝试也会导致错误消息。您无法删除正在等待确认的订阅。Amazon SNS 会在 48 小时后自动删除未确认的订阅。
重要
删除的 Amazon SNS 主题将无法恢复。如果您有该主题的有效订阅,则删除主题不会删除其订阅。建议在删除主题之前先删除订阅。
订阅删除后将无法恢复。已发布的 Amazon SNS 主题的通知将不会发送到该已删除的订阅。您必须重新创建订阅才能接收通知。
使用 Amazon Web Services 管理控制台删除 Amazon SNS 主题或订阅
删除 Amazon SNS 主题或订阅可确保高效的资源管理、防止不必要的使用并保持 Amazon SNS 控制台井然有序。此步骤有助于避免闲置资源带来的潜在成本,并通过移除不再需要的主题或订阅来简化管理。
要使用 Amazon Web Services 管理控制台删除主题
登录 Amazon SNS 控制台
。 -
在左侧导航窗格中,选择主题。
-
在 Topics(主题)页面上,选择一个主题,然后选择 Delete(删除)。
-
在 Delete topic(删除主题)对话框中,输入
delete me,然后选择 Delete(删除)。控制台将删除该主题。
使用 Amazon Web Services 管理控制台删除订阅
登录 Amazon SNS 控制台
。 -
在左侧导航窗格中,选择订阅。
-
在订阅页面上,选择状态为已确认的订阅,然后选择删除。
-
在 Delete subscription(删除订阅)对话框中,选择 Delete(删除)。
控制台将删除该订阅。
使用 Amazon SDK 删除订阅和主题
要使用 Amazon SDK,您必须使用您的凭证对其进行配置。有关更多信息,请参阅《Amazon SDKs and Tools Reference Guide》中的 The shared config and credentials files。
以下代码示例演示如何使用 DeleteTopic。
- .NET
-
- 适用于 .NET 的 Amazon SDK
-
注意
查看 GitHub,了解更多信息。在 Amazon 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 按主题 ARN 删除主题。
/// <summary> /// Delete a topic by its topic ARN. /// </summary> /// <param name="topicArn">The ARN of the topic.</param> /// <returns>True if successful.</returns> public async Task<bool> DeleteTopicByArn(string topicArn) { var deleteResponse = await _amazonSNSClient.DeleteTopicAsync( new DeleteTopicRequest() { TopicArn = topicArn }); return deleteResponse.HttpStatusCode == HttpStatusCode.OK; }-
有关 API 详细信息,请参阅《适用于 .NET 的 Amazon SDK API Reference》中的 DeleteTopic。
-
- C++
-
- SDK for C++
-
注意
查看 GitHub,了解更多信息。在 Amazon 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 //! Delete an Amazon Simple Notification Service (Amazon SNS) topic. /*! \param topicARN: The Amazon Resource Name (ARN) for an Amazon SNS topic. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::SNS::deleteTopic(const Aws::String &topicARN, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::SNS::SNSClient snsClient(clientConfiguration); Aws::SNS::Model::DeleteTopicRequest request; request.SetTopicArn(topicARN); const Aws::SNS::Model::DeleteTopicOutcome outcome = snsClient.DeleteTopic(request); if (outcome.IsSuccess()) { std::cout << "Successfully deleted the Amazon SNS topic " << topicARN << std::endl; } else { std::cerr << "Error deleting topic " << topicARN << ":" << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }-
有关 API 详细信息,请参阅《适用于 C++ 的 Amazon SDK API Reference》中的 DeleteTopic。
-
- CLI
-
- Amazon CLI
-
删除 SNS 主题
以下
delete-topic示例将删除指定的 SNS 主题。aws sns delete-topic \ --topic-arn"arn:aws:sns:us-west-2:123456789012:my-topic"此命令不生成任何输出。
-
有关 API 详细信息,请参阅《Amazon CLI Command Reference》中的 DeleteTopic
。
-
- Go
-
- 适用于 Go 的 SDK V2
-
注意
查看 GitHub,了解更多信息。在 Amazon 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 import ( "context" "encoding/json" "log" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/sns" "github.com/aws/aws-sdk-go-v2/service/sns/types" ) // SnsActions encapsulates the Amazon Simple Notification Service (Amazon SNS) actions // used in the examples. type SnsActions struct { SnsClient *sns.Client } // DeleteTopic delete an Amazon SNS topic. func (actor SnsActions) DeleteTopic(ctx context.Context, topicArn string) error { _, err := actor.SnsClient.DeleteTopic(ctx, &sns.DeleteTopicInput{ TopicArn: aws.String(topicArn)}) if err != nil { log.Printf("Couldn't delete topic %v. Here's why: %v\n", topicArn, err) } return err }-
有关 API 详细信息,请参阅《适用于 Go 的 Amazon SDK API Reference》中的 DeleteTopic
。
-
- Java
-
- 适用于 Java 的 SDK 2.x
-
注意
查看 GitHub,了解更多信息。在 Amazon 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.sns.SnsClient; import software.amazon.awssdk.services.sns.model.DeleteTopicRequest; import software.amazon.awssdk.services.sns.model.DeleteTopicResponse; import software.amazon.awssdk.services.sns.model.SnsException; /** * 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 DeleteTopic { public static void main(String[] args) { final String usage = """ Usage: <topicArn> Where: topicArn - The ARN of the topic to delete. """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String topicArn = args[0]; SnsClient snsClient = SnsClient.builder() .region(Region.US_EAST_1) .build(); System.out.println("Deleting a topic with name: " + topicArn); deleteSNSTopic(snsClient, topicArn); snsClient.close(); } 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); } } }-
有关 API 详细信息,请参阅《Amazon SDK for Java 2.x API Reference》中的 DeleteTopic。
-
- JavaScript
-
- SDK for JavaScript (v3)
-
注意
查看 GitHub,了解更多信息。在 Amazon 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 在单独的模块中创建客户端并将其导出。
import { SNSClient } from "@aws-sdk/client-sns"; // The AWS Region can be provided here using the `region` property. If you leave it blank // the SDK will default to the region set in your AWS config. export const snsClient = new SNSClient({});导入 SDK 和客户端模块,然后调用 API。
import { DeleteTopicCommand } from "@aws-sdk/client-sns"; import { snsClient } from "../libs/snsClient.js"; /** * @param {string} topicArn - The ARN of the topic to delete. */ export const deleteTopic = async (topicArn = "TOPIC_ARN") => { const response = await snsClient.send( new DeleteTopicCommand({ TopicArn: topicArn }), ); console.log(response); // { // '$metadata': { // httpStatusCode: 200, // requestId: 'a10e2886-5a8f-5114-af36-75bd39498332', // extendedRequestId: undefined, // cfId: undefined, // attempts: 1, // totalRetryDelay: 0 // } // } };-
有关 API 详细信息,请参阅《适用于 JavaScript 的 Amazon SDK API Reference》中的 DeleteTopic。
- Kotlin
-
- SDK for Kotlin
-
注意
查看 GitHub,了解更多信息。在 Amazon 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 suspend fun deleteSNSTopic(topicArnVal: String) { val request = DeleteTopicRequest { topicArn = topicArnVal } SnsClient.fromEnvironment { region = "us-east-1" }.use { snsClient -> snsClient.deleteTopic(request) println("$topicArnVal was successfully deleted.") } }-
有关 API 详细信息,请参阅《Amazon SDK for Kotlin API Reference》中的 DeleteTopic
。
-
- PHP
-
- 适用于 PHP 的 SDK
-
注意
查看 GitHub,了解更多信息。在 Amazon 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 require 'vendor/autoload.php'; use Aws\Exception\AwsException; use Aws\Sns\SnsClient; /** * Deletes an 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()); }-
有关 API 详细信息,请参阅《适用于 PHP 的 Amazon SDK API Reference》中的 DeleteTopic。
-
- Python
-
- 适用于 Python 的 SDK(Boto3)
-
注意
查看 GitHub,了解更多信息。在 Amazon 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 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 @staticmethod 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-
有关 API 详细信息,请参阅《Amazon SDK for Python (Boto3) API Reference》中的 DeleteTopic。
-
- SAP ABAP
-
- 适用于 SAP ABAP 的 SDK
-
注意
查看 GitHub,了解更多信息。在 Amazon 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 TRY. lo_sns->deletetopic( iv_topicarn = iv_topic_arn ). MESSAGE 'SNS topic deleted.' TYPE 'I'. CATCH /aws1/cx_snsnotfoundexception. MESSAGE 'Topic does not exist.' TYPE 'E'. ENDTRY.-
有关 API 详细信息,请参阅《Amazon SDK for SAP ABAP API Reference》中的 DeleteTopic。
-
- Swift
-
- 适用于 Swift 的 SDK
-
注意
查看 GitHub,了解更多信息。在 Amazon 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 import AWSSNS let config = try await SNSClient.SNSClientConfiguration(region: region) let snsClient = SNSClient(config: config) _ = try await snsClient.deleteTopic( input: DeleteTopicInput(topicArn: arn) )-
有关 API 详细信息,请参阅《Amazon SDK for Swift API Reference》中的 DeleteTopic
。
-