When a SNS topic is deleted, subscriptions are not deleted automatically (bug?)

1

According to the documentation (https://docs.aws.amazon.com/sns/latest/dg/sns-delete-subscription-topic.html) when a topic is deleted, all associated subscriptions should be automatically deleted as well.

However this does not seem to be the case. We have an application using SNS for push notifications. When a topic is deleted, associated subscriptions are NOT deleted and must be deleted manually.

Here's a test program to reproduce the problem; in order to run this you must provide a valid AWS key id, secret, and region, and a valid SNS platform endpoint.

import time
import boto3

AWS_KEY_ID = "AKIAIOSFODNN7EXAMPLE"
AWS_KEY_SECRET = "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
AWS_REGION = "eu-west-1"
ENDPOINT = "arn:aws:sns:eu-west-1:1234567890:endpoint/GCM/Application/12345678-abcd-9012-efgh-345678901234"

session = boto3.Session(
    aws_access_key_id=AWS_KEY_ID,
    aws_secret_access_key=AWS_KEY_SECRET,
    region_name=AWS_REGION,
)
sns_client = session.client("sns")

# Create topic, add subscription
topic_arn = sns_client.create_topic(Name="Test_bug")["TopicArn"]
subscription = sns_client.subscribe(
    TopicArn=topic_arn, Protocol="application", Endpoint=ENDPOINT
)["SubscriptionArn"]

# Delete topic
sns_client.delete_topic(TopicArn=topic_arn)
time.sleep(60)

# Check if subscription was deleted
try:
    sns_client.get_subscription_attributes(SubscriptionArn=subscription)
    print("Unexpected behaviour -- subscription still exists")
except sns_client.exceptions.NotFoundException:
    print("Expected behaviour -- subscription deleted")

Running this shows that the subscription is not deleted automatically when the topic is deleted.

Is this a bug in AWS ?

asked 8 months ago271 views
2 Answers
0

Hi,

re:Post is not the right place to report bugs: AWS service teams usually do not monitor this site to get aware of those reports.

The right place to go is the AWS service console, which has features to open such tickets for any service. So, please, go there to announce this issue. Additionally, you will be able to assign it to the right priority level to raise more attention if needed.

Best,

Didier

profile pictureAWS
EXPERT
answered 8 months ago
  • "Technical support is unavailable under the Basic Support plan", so no luck.

  • If there is a way to submit a bug report other than creating a "support case" (which I apparently cannot do) please let me know..

0

Hello.

I have confirmed its operation as well.
Indeed, the subscriptions did not disappear, but remained.
The following shell script can be used to delete subscriptions not associated with SNS topics.

#!/bin/bash

# Get a list of SNS topics
topics=$(aws sns list-topics | jq -r '.Topics[].TopicArn')

# Get a list of SNS subscriptions
subscriptions=$(aws sns list-subscriptions)

# Remove subscriptions not tied to a topic
echo $subscriptions | jq -c '.Subscriptions[]' | while read subscription; do
  subscriptionArn=$(echo $subscription | jq -r '.SubscriptionArn')
  topicArn=$(echo $subscription | jq -r '.TopicArn')

  # If the topicArn of the subscription is not included in the topics, delete it.
  if ! echo $topics | grep $topicArn > /dev/null
  then
    aws sns unsubscribe --subscription-arn $subscriptionArn
    echo "delete it: $subscriptionArn"
  fi
done
profile picture
EXPERT
answered 8 months ago
profile pictureAWS
EXPERT
reviewed 8 months ago
  • Note that aws sns list-subscriptions will only return a limited set of subscriptions (up to 100). Thus this script will not work as expected when there is a large number of subscriptions.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions