Deleting an Amazon SNS subscription and topic - Amazon Simple Notification Service
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).

Deleting an Amazon SNS subscription and topic

You can delete a subscription from an Amazon SNS topic, or you can delete the whole topic. Note that you can't delete a subscription that's pending confirmation. After three days, Amazon SNS deletes the unconfirmed subscription automatically.

To delete an Amazon SNS subscription and topic using the Amazon Web Services Management Console

To delete a subscription using the Amazon Web Services Management Console
  1. Sign in to the Amazon SNS console.

  2. In the left navigation pane, choose Subscriptions.

  3. On the Subscriptions page, select a subscription with a Status of Confirmed, and then choose Delete.

  4. In the Delete subscription dialog box, choose Delete.

    The console deletes the subscription.

When you delete a topic, Amazon SNS deletes the subscriptions associated with the topic.

To delete a topic using the Amazon Web Services Management Console
  1. Sign in to the Amazon SNS console.

  2. In the left navigation pane, choose Topics.

  3. On the Topics page, select a topic, and then choose Delete.

  4. In the Delete topic dialog box, enter delete me, and then choose Delete.

    The console deletes the topic.

To delete a subscription and topic using an Amazon SDK

To use an Amazon SDK, you must configure it with your credentials. For more information, see The shared config and credentials files in the Amazon SDKs and Tools Reference Guide.

The following code examples show how to delete an Amazon SNS topic and all subscriptions to that topic.

.NET
Amazon SDK for .NET
Note

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

using System; using System.Threading.Tasks; using Amazon.SimpleNotificationService; /// <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:012345678901:ExampleSNSTopic"; IAmazonSimpleNotificationService client = new AmazonSimpleNotificationServiceClient(); var response = await client.DeleteTopicAsync(topicArn); } }
  • For API details, see DeleteTopic in Amazon SDK for .NET API Reference.

C++
SDK for C++
Note

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

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);
  • For API details, see DeleteTopic in Amazon SDK for C++ 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.

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); } }
  • For API details, see DeleteTopic 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 in a separate module and export it.

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 };

Import the SDK and client modules and call the 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
Note

This is prerelease documentation for a feature in preview release. It is subject to change.

Note

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

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.") } }
  • For API details, see DeleteTopic in Amazon SDK for Kotlin API reference.

PHP
SDK for PHP
Note

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

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()); }
  • For API details, see DeleteTopic in Amazon SDK for PHP 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 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
  • For API details, see DeleteTopic in Amazon SDK for Python (Boto3) API Reference.

SAP ABAP
SDK for SAP ABAP
Note

This documentation is for an SDK in developer preview release. The SDK is subject to change and is not recommended for use in production.

Note

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

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.
  • For API details, see DeleteTopic in Amazon SDK for SAP ABAP API reference.