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 entire topic.
Once an Amazon SNS topic is deleted, all subscriptions associated with that topic are also
deleted. This means that any confirmed subscription that was previously established for the
deleted topic will no longer exist.
If a subscriber attempts to publish a message to the deleted topic, the publisher will
receive an error message indicating that the topic doesn't exist. Similarly, any attempt to
subscribe to the deleted topic will also result in an error message.
You can't delete a subscription that's pending confirmation. Amazon SNS automatically deletes
unconfirmed subscriptions after 48 hours.
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
Sign in to the Amazon SNS console.
-
In the left navigation pane, choose Subscriptions.
-
On the Subscriptions page, select a subscription with a
Status of Confirmed, and then
choose Delete.
-
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
Sign in to the Amazon SNS console.
-
In the left navigation pane, choose Topics.
-
On the Topics page, select a topic, and then choose
Delete.
-
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
-
Delete a topic by its topic 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;
}
- C++
-
- SDK for C++
-
//! 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();
}
- 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);
}
}
- JavaScript
-
- SDK for JavaScript (v3)
-
Create the client in a separate module and export it.
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({});
Import the SDK and client modules and call the 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
// }
// }
};
- Kotlin
-
- SDK for Kotlin
-
This is prerelease documentation for a feature in preview release. It is subject to change.
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.")
}
}
- 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());
}
- 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
@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
- SAP ABAP
-
- SDK for SAP ABAP
-
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.