我安装了rabbitmqadmin,并且能够列出所有的交换机和队列。如何使用rabbitmqadmin或rabbitmqctl删除所有的队列。
当前回答
我尝试了rabbitmqctl和reset命令,但它们非常慢。
这是我找到的最快的方法(替换用户名和密码):
#!/bin/bash
# Stop on error
set -eo pipefail
USER='guest'
PASSWORD='guest'
curl -sSL -u $USER:$PASSWORD http://localhost:15672/api/queues/%2f/ | jq '.[].name' | sed 's/"//g' | xargs -L 1 -I@ curl -XDELETE -sSL -u $USER:$PASSWORD http://localhost:15672/api/queues/%2f/@
# To also delete exchanges uncomment next line
# curl -sSL -u $USER:$PASSWORD http://localhost:15672/api/exchanges/%2f/ | jq '.[].name' | sed 's/"//g' | xargs -L 1 -I@ curl -XDELETE -sSL -u $USER:$PASSWORD http://localhost:15672/api/exchanges/%2f/@
注意:这只适用于默认vhost /
其他回答
下面是一个更快的版本(使用并行安装sudo apt-get install parallel),扩展了@admenva的优秀答案
parallel -j 50 rabbitmqadmin -H YOUR_HOST_OR_LOCALHOST -q delete queue name={}::: $(rabbitmqadmin -H YOUR_HOST_OR_LOCALHOST -f tsv -q list queue name)
Okay, important qualifier for this answer: The question does ask to use either rabbitmqctl OR rabbitmqadmin to solve this, my answer needed to use both. Also, note that this was tested on MacOS 10.12.6 and the versions of the rabbitmqctl and rabbitmqadmin that are installed when installing rabbitmq with Homebrew and which is identified with brew list --versions as rabbitmq 3.7.0 rabbitmqctl list_queues -p <VIRTUAL_HOSTNAME> name | sed 1,2d | xargs -I qname rabbitmqadmin --vhost <VIRTUAL_HOSTNAME> delete queue name=qname
首先,列出你的队列:
Rabbitmqadmin列表队列名称
然后从列表中,你需要逐个手动删除它们:
删除队列名
由于输出格式,您无法从列表队列中grep响应。或者,如果你只是在寻找一种方法来清除所有内容(读取:重置所有设置,将安装返回到默认状态),使用:
rabbitmqctl stop_app
rabbitmqctl reset # Be sure you really want to do this!
rabbitmqctl start_app
如果你没有安装rabbitmqadmin,尝试使用rabbitmqctl清除队列:
rabbitmqctl list_queues | awk '{print $1}' | xargs -L1 rabbitmqctl purge_queue . txt
如https://stackoverflow.com/a/52002145/3278855
要实现自动化,可以使用curl:
curl -X PUT --data '{"pattern":".*","apply-to":"all","definition":{"expires":1},"priority":0}' -u guest:guest 'http://localhost:15672/api/policies/%2f/clear' && \
curl -X DELETE -u guest:guest 'http://localhost:15672/api/policies/%2f/clear'
请注意%2f是默认的v主机名(/),guest:guest是登录名:密码