我安装了rabbitmqadmin,并且能够列出所有的交换机和队列。如何使用rabbitmqadmin或rabbitmqctl删除所有的队列。


当前回答

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

其他回答

如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是登录名:密码

我尝试了上面的代码段,但我没有做任何流。

Sudo rabbitmqctl list_queues | awk '{print $1}' > queues.txt;对于$(cat queue .txt)中的行;执行sudo rabbitmqctl delete_queue "$line";完成了。

我生成一个包含所有队列名称的文件,并逐行循环删除它们。对于循环,当读取…不是为了我。它总是在第一个队列名处停止。

如果安装rabbitmqadmin有问题,请先安装python。

类unix操作系统用户需要将rabbitmqadmin复制到PATH中的某个目录,例如/usr/local/bin。

Windows用户将需要确保Python在他们的PATH上,并调用rabbitmqadmin作为Python .exe rabbitmqadmin。

然后

浏览http://{hostname}:15672/cli/rabbitmqadmin进行下载。 进入包含的文件夹,然后以管理员权限运行cmd

列出队列 Python rabbitmqadmin列表队列。

删除队列 python rabbitmqadmin删除队列名称=队列名称

删除所有队列

1-声明政策

python rabbitmqadmin declare policy name='expire_all_policies' pattern=.* definition={\"expires\":1} apply-to=queues

2-删除策略

python rabbitmqadmin  delete policy name='expire_all_policies'

你可以这样使用rabbitmqctl eval:

rabbitmqctl eval 'IfUnused = false, IfEmpty = true, MatchRegex = 
<<"^prefix-">>, [rabbit_amqqueue:delete(Q, IfUnused, IfEmpty) || Q <- 
rabbit_amqqueue:list(), re:run(element(4, element(2, Q)), MatchRegex) 
=/= nomatch ].' 

上述操作将删除所有有名称的vhosts中的所有空队列 以“前缀-”开头。 你可以编辑变量IfUnused, IfEmpty, 和MatchRegex根据您的要求。

我做了一个deleteRabbitMqQs.sh,它接受参数来搜索队列列表,只选择与您想要的模式匹配的参数。如果你没有提供参数,它会将它们全部删除!它向您显示即将删除的队列列表,让您在执行任何破坏性操作之前退出。

for word in "$@"
do
        args=true
        newQueues=$(rabbitmqctl list_queues name | grep "$word")
        queues="$queues
$newQueues"
done
if [ $# -eq 0 ]; then
        queues=$(rabbitmqctl list_queues name | grep -v "\.\.\.")
fi

queues=$(echo "$queues" | sed '/^[[:space:]]*$/d')

if [ "x$queues" == "x" ]; then
        echo "No queues to delete, giving up."
        exit 0
fi

read -p "Deleting the following queues:
${queues}
[CTRL+C quit | ENTER proceed]
"

while read -r line; do
        rabbitmqadmin delete queue name="$line"
done <<< "$queues"

如果想要对传入的参数进行不同的匹配,可以在第4行中更改grep。当删除所有队列时,它不会删除其中有三个连续空格的队列,因为我认为这种情况比使用rabbitmqctl打印不同语言的输出更少见。

享受吧!