简体   繁体   English

使用boto3批量删除sqs队列

[英]Bulk delete sqs queues using boto3

I want to delete about 300-400 sqs queues.我想删除大约 300-400 sqs 队列。 i am trying to do it using lambda function and boto3.我正在尝试使用 lambda function 和 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 ','.列出队列会给我一个由“,”分隔的 URL 列表。

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.我想遍历列表并删除 URL,因为 delete API 调用只需要字符串作为输入。 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.谢谢。

You can iterate directly over the queue urls, rather then their indices.您可以直接遍历队列 url,而不是它们的索引。 Thus it should be:因此它应该是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM