Home > Software design >  Bulk delete sqs queues using boto3
Bulk delete sqs queues using boto3

Time:03-04

I want to delete about 300-400 sqs queues. i am trying to do it using lambda function and boto3. My programming skills are not that good. I am trying to do something like this

def delete_queues (event, context):
    response = client.list_queues(
        QueueNamePrefix='aaa-bbb-ccc'
    )

    
    for i in response:
        del_queue = client.delete_queue(QueueUrl=response[i])

Listing the queues gives me a list of URLs separated by a ','.

Response
{
  "QueueUrls": [
    "https://ap-southeast-2.queue.amazonaws.com/xxxx/aaa-bbb-ccc.fifo",
    "https://ap-southeast-2.queue.amazonaws.com/xxxx/aaa-bbb-ccc.fifo",
    "https://ap-southeast-2.queue.amazonaws.com/xxxx/aaa-bbb-ccc.fifo",
    "https://ap-southeast-2.queue.amazonaws.com/xxxx/aaa-bbb-ccc.fifo",
    "https://ap-southeast-2.queue.amazonaws.com/xxxx/aaa-bbb-ccc.fifo",
    "https://ap-southeast-2.queue.amazonaws.com/xxxx/aaa-bbb-ccc.fifo",
    "https://ap-southeast-2.queue.amazonaws.com/xxxx/aaa-bbb-ccc.fifo",
    "https://ap-southeast-2.queue.amazonaws.com/xxxx/aaa-bbb-ccc.fifo",
    "https://ap-southeast-2.queue.amazonaws.com/xxxx/aaa-bbb-ccc.fifo",
    "https://ap-southeast-2.queue.amazonaws.com/xxxx/aaa-bbb-ccc.fifo"]

I want to loop through the list and delete the URLs because the delete API call only wants string as input. When i run this it gives me the below error:

"errorMessage": "Parameter validation failed:\nInvalid type for parameter QueueUrl, value: ['https://ap-southeast-2.queue.amazonaws.com/xxxx/aaa-bbb-ccc.fifo', 'https://ap-southeast-2.queue.amazonaws.com/xxxx/aaa-bbb-ccc.fifo', 'https://ap-southeast-2.queue.amazonaws.com/xxxx/aaa-bbb-ccc.fifo', 'https://ap-southeast-2.queue.amazonaws.com/xxxx/aaa-bbb-ccc.fifo', 'https://ap-southeast-2.queue.amazonaws.com/xxxx/aaa-bbb-ccc.fifo', 'https://ap-southeast-2.queue.amazonaws.com/xxxx/aaa-bbb-ccc.fifo'], type: <class 'list'>, valid types: <class 'str'>"

Could someone please help me correct this. Thanks.

CodePudding user response:

You can iterate directly over the queue urls, rather then their indices. Thus it should be:

for sqs_url in response['QueueUrls']:
   print(f'Deleting {sqs_url}...')
   del_queue = client.delete_queue(QueueUrl=sqs_url)
  • Related