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


当前回答

我做了一个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打印不同语言的输出更少见。

享受吧!

其他回答

我尝试了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 rabbitmqctl list_queues | awk '{print $1}' | xargs -I qn sudo rabbitmqctl delete_queue qn

有一种方法可以在不使用脚本和完全重置的情况下删除所有队列和交换机。您可以从管理界面删除并重新创建虚拟主机。这甚至适用于vhost /。

您唯一需要恢复的是新创建的vhost的权限。

这是我使用的方法。它简单、清晰、有效。 文件如下:

Vhost=the_vhost_name
User=user_name
Password=the_passworld

for i in `rabbitmqctl list_queues -p $Vhost |  awk '{ print $1 }'`
do
    echo "queu_name: $i"
    curl  -u $User:$Passworld -H "content-type:application/json"  -XDELETE http://localhost:15672/api/queues/$Vhost/$i
done 

在Rabbit 3.7.10版本中,你可以以root权限运行以下命令:

rabbitmqctl list_queues | awk '{ print $1 }' | xargs -L1 rabbitmqctl delete_queue