amazon-web-servicesamazon-sqsamazon-sns

How to delete AWS SNS topics and SQS queues in bulk with AWS CLI by a pattern?


I have many topics and queues named in the same pattern. Now I need to delete them all in bulk using some pattern with a wildcard. The AWS CLI only allows deleting one by one. Same in the AWS Console - the list of queues and topics have radio boxes in their rows allowing to delete one by one only. How to delete multiple items at once by a pattern? What script can I use for that?


Solution

  • Using the CLI in your shell or CloudShell you can do something like:

    topics=$(aws sns list-topics --query 'Topics[*]' --output text | grep <YOUR_PATTERN>)
    for topic in $topics 
    do 
      echo "Deleting topic" $topic
      aws sns delete-topic --topic-arn $topic
    done
    

    Do the same with the CLI commands for sqs.