删除 RabbitMQ 中的队列

发布于 2024-11-25 02:59:11 字数 129 浏览 0 评论 0 原文

我有几个使用 RabbitMQ 运行的队列。其中有一些现在没有用了,如何删除它们?不幸的是我没有设置auto_delete选项。

如果现在设置的话会被删除吗?

现在有办法删除这些队列吗?

I have a few queues running with RabbitMQ. A few of them are of no use now, how can I delete them? Unfortunately I had not set the auto_delete option.

If I set it now, will it be deleted?

Is there a way to delete those queues now?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(17

淡淡離愁欲言轉身 2024-12-02 02:59:12

我采用了不同的方式,因为我只能访问管理网页。我创建了简单的“片段”,它在 Javascript 中删除队列。这是:

function zeroPad(num, places) {
  var zero = places - num.toString().length + 1;
  return Array(+(zero > 0 && zero)).join("0") + num;
}
var queuePrefix = "PREFIX"
for(var i=0; i<255; i++){ 
   var queueid = zeroPad(i, 4);
   $.ajax({url: '/api/queues/vhost/'+queuePrefix+queueid, type: 'DELETE', success: function(result) {console.log('deleted '+queuePrefix+queueid)}});
}

我的所有队列的格式为:PREFIX_0001 到 PREFIX_0XXX

I did it different way, because I only had access to management webpage. I created simple "snippet" which delete queues in Javascript. Here it is:

function zeroPad(num, places) {
  var zero = places - num.toString().length + 1;
  return Array(+(zero > 0 && zero)).join("0") + num;
}
var queuePrefix = "PREFIX"
for(var i=0; i<255; i++){ 
   var queueid = zeroPad(i, 4);
   $.ajax({url: '/api/queues/vhost/'+queuePrefix+queueid, type: 'DELETE', success: function(result) {console.log('deleted '+queuePrefix+queueid)}});
}

All my queues was in format: PREFIX_0001 to PREFIX_0XXX

轻许诺言 2024-12-02 02:59:12

如果您使用 C#,您可以像这样使用 HareDu API:

var result = await _services.GetService<IBrokerObjectFactory>()
    .DeleteQueue("queue", "vhost");

...或

var result = await _services.GetService<IBrokerObjectFactory>()
    .DeleteQueue("queue", "vhost", x =>
    {
        x.WhenHasNoConsumers();
        x.WhenEmpty();
    });

https://github.com/ahives/HareDu2/blob/master/docs/queue-delete.md

If you are using C#, you can use HareDu API like so:

var result = await _services.GetService<IBrokerObjectFactory>()
    .DeleteQueue("queue", "vhost");

...or

var result = await _services.GetService<IBrokerObjectFactory>()
    .DeleteQueue("queue", "vhost", x =>
    {
        x.WhenHasNoConsumers();
        x.WhenEmpty();
    });

https://github.com/ahives/HareDu2/blob/master/docs/queue-delete.md

恏ㄋ傷疤忘ㄋ疼 2024-12-02 02:59:12

删除队列及其所有消息

sudo rabbitmqctl delete_queue --vhost <vhost-name> <queue-name>

仅删除消息并保留队列

sudo rabbitmqctl purge_queue --vhost <vhost-name> <queue-name>

如果您不使用任何虚拟主机,则从上述命令中删除 --vhost

注意:

  1. 替换为虚拟主机的名称
  2. 替换为要删除的队列的名称

您可以使用 sudorabbitmqctl list_queues --vhost 列出所有队列。

Delete a queue and all its messages

sudo rabbitmqctl delete_queue --vhost <vhost-name> <queue-name>

Delete only messaged and keep the queue

sudo rabbitmqctl purge_queue --vhost <vhost-name> <queue-name>

If you are not using any virtual host then remove --vhost <vhost-name> from above commands.

Note:

  1. Replace <vhost-name> with name of the virtual host
  2. Replace <queue-name> with name of the queue you want to delete

You can use sudo rabbitmqctl list_queues --vhost <vhost-name> to list all the queues.

乙白 2024-12-02 02:59:11

如果您不关心管理数据库中的数据;即usersvhostsmessages等,而不是其他队列,那么你可以重置 通过命令行按顺序运行以下命令:

警告:除了队列之外,这还会删除任何用户< code>vhosts,您已在 RabbitMQ 服务器上进行配置;并将删除任何持久消息

rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl start_app

rabbitmq 文档reset 命令:

将 RabbitMQ 节点返回到其原始状态。

从它所属的任何集群中删除节点,并从中删除所有数据
管理数据库,例如配置的用户和虚拟主机,以及
删除所有持久消息。

所以,使用它要小心。

If you do not care about the data in management database; i.e. users, vhosts, messages etc., and neither about other queues, then you can reset via commandline by running the following commands in order:

WARNING: In addition to the queues, this will also remove any users and vhosts, you have configured on your RabbitMQ server; and will delete any persistent messages

rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl start_app

The rabbitmq documentation says that the reset command:

Returns a RabbitMQ node to its virgin state.

Removes the node from any cluster it belongs to, removes all data from
the management database, such as configured users and vhosts, and
deletes all persistent messages.

So, be careful using it.

倒数 2024-12-02 02:59:11
import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(
               'localhost'))
channel = connection.channel()

channel.queue_delete(queue='queue-name')

connection.close()

安装 pika 软件包如下:

$ sudo pip install pika==0.9.8

安装依赖于 pip 和 git-core 软件包,您可能需要先安装它们。

在 Ubuntu 上:

$ sudo apt-get install python-pip git-core

在 Debian 上:

$ sudo apt-get install python-setuptools git-core
$ sudo easy_install pip

在 Windows 上: 要安装 easy_install,请运行 MS Windows Installer for setuptools

> easy_install pip
> pip install pika==0.9.8
import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(
               'localhost'))
channel = connection.channel()

channel.queue_delete(queue='queue-name')

connection.close()

Install pika package as follows

$ sudo pip install pika==0.9.8

The installation depends on pip and git-core packages, you may need to install them first.

On Ubuntu:

$ sudo apt-get install python-pip git-core

On Debian:

$ sudo apt-get install python-setuptools git-core
$ sudo easy_install pip

On Windows: To install easy_install, run the MS Windows Installer for setuptools

> easy_install pip
> pip install pika==0.9.8
可爱咩 2024-12-02 02:59:11

在 RabbitMQ 版本中 > 3.0,如果启用了rabbitmq_management插件,您还可以使用HTTP API。只需确保将内容类型设置为“application/json”并提供虚拟主机和队列名称:

IE 使用带有虚拟主机“test”和队列名称“testqueue”的curl:

$ curl -i -u guest:guest -H "content-type:application/json" -XDELETE http://localhost:15672/api/queues/test/testqueue
HTTP/1.1 204 No Content
Server: MochiWeb/1.1 WebMachine/1.9.0 (someone had painted it blue)
Date: Tue, 16 Apr 2013 10:37:48 GMT
Content-Type: application/json
Content-Length: 0

In RabbitMQ versions > 3.0, you can also utilize the HTTP API if the rabbitmq_management plugin is enabled. Just be sure to set the content-type to 'application/json' and provide the vhost and queue name:

I.E. Using curl with a vhost 'test' and queue name 'testqueue':

$ curl -i -u guest:guest -H "content-type:application/json" -XDELETE http://localhost:15672/api/queues/test/testqueue
HTTP/1.1 204 No Content
Server: MochiWeb/1.1 WebMachine/1.9.0 (someone had painted it blue)
Date: Tue, 16 Apr 2013 10:37:48 GMT
Content-Type: application/json
Content-Length: 0
世界如花海般美丽 2024-12-02 02:59:11

rabbitmqadmin,它很适合从控制台工作。

如果你通过ssh/登录到安装了rabbit的服务器,你可以从以下位置下载它

http://{server}:15672/cli/rabbitmqadmin

并将其保存到/usr/local/bin/rabbitmqadmin

然后你就可以运行

rabbitmqadmin -u {user} -p {password} -V {vhost} delete queue name={name}

通常它需要sudo。

如果您想避免输入用户名和密码,您可以使用配置

rabbitmqadmin -c /var/lib/rabbitmq/.rabbitmqadmin.conf -V {vhost} delete queue name={name}

所有这些假设您有文件 ** /var/lib/rabbitmq/.rabbitmqadmin.conf** 并且有裸露的最小

hostname = localhost
port = 15672
username = {user}
password = {password}

编辑:截至@的评论user299709,指出用户必须在 Rabbit 中标记为“管理员”可能会有所帮助。 (https://www.rabbitmq.com/management.html)

There is rabbitmqadmin which is nice to work from console.

If you ssh/log into server where you have rabbit installed, you can download it from:

http://{server}:15672/cli/rabbitmqadmin

and save it into /usr/local/bin/rabbitmqadmin

Then you can run

rabbitmqadmin -u {user} -p {password} -V {vhost} delete queue name={name}

Usually it requires sudo.

If you want to avoid typing your user name and password, you can use config

rabbitmqadmin -c /var/lib/rabbitmq/.rabbitmqadmin.conf -V {vhost} delete queue name={name}

All that under assumption that you have file ** /var/lib/rabbitmq/.rabbitmqadmin.conf** and have bare minumum

hostname = localhost
port = 15672
username = {user}
password = {password}

EDIT: As of comment from @user299709, it might be helpful to point out that user must be tagged as 'administrator' in rabbit. (https://www.rabbitmq.com/management.html)

戈亓 2024-12-02 02:59:11

使用运行 RMQ 服务器的主机中的所有默认值快速删除队列的简短摘要:

curl -O http://localhost:15672/cli/rabbitmqadmin
chmod u+x rabbitmqadmin
./rabbitmqadmin delete queue name=myQueueName

要删除与给定虚拟主机中的模式匹配的所有队列(例如,根虚拟主机中包含“amq.gen”):

rabbitmqctl -p / list_queues | grep 'amq.gen' | cut -f1 -d
\t' | xargs -I % ./rabbitmqadmin -V / delete queue name=%

A short summary for quick queue deletion with all default values from the host that is running RMQ server:

curl -O http://localhost:15672/cli/rabbitmqadmin
chmod u+x rabbitmqadmin
./rabbitmqadmin delete queue name=myQueueName

To delete all queues matching a pattern in a given vhost (e.g. containing 'amq.gen' in the root vhost):

rabbitmqctl -p / list_queues | grep 'amq.gen' | cut -f1 -d
\t' | xargs -I % ./rabbitmqadmin -V / delete queue name=%
还不是爱你 2024-12-02 02:59:11

您可以使用queue.declare断言队列存在(如果不存在则创建它)。如果您最初将 auto-delete 设置为 false,则使用 autodelete true 再次调用queue.declare 将导致软错误,并且代理将关闭通道。

您现在需要使用queue.delete来删除它。

有关详细信息,请参阅 API 文档:

如果您使用其他客户端,则需要找到等效的方法。因为它是协议的一部分,所以它应该在那里,并且它可能是 Channel 或同等内容的一部分。

您可能还想查看文档的其余部分,特别是入门部分涵盖了许多常见用例。

最后,如果您有问题并且在其他地方找不到答案,您应该尝试在 RabbitMQ 讨论 邮件列表。开发人员尽力回答那里提出的所有问题。

You assert that a queue exists (and create it if it does not) by using queue.declare. If you originally set auto-delete to false, calling queue.declare again with autodelete true will result in a soft error and the broker will close the channel.

You need to use queue.delete now in order to delete it.

See the API documentation for details:

If you use another client, you'll need to find the equivalent method. Since it's part of the protocol, it should be there, and it's probably part of Channel or the equivalent.

You might also want to have a look at the rest of the documentation, in particular the Geting Started section which covers a lot of common use cases.

Finally, if you have a question and can't find the answer elsewhere, you should try posting on the RabbitMQ Discuss mailing list. The developers do their best to answer all questions asked there.

旧人 2024-12-02 02:59:11

另一种选择是启用 management_plugin 并通过浏览器连接到它。您可以查看所有队列及其相关信息。从该界面删除队列是可能且简单的。

Another option would be to enable the management_plugin and connect to it over a browser. You can see all queues and information about them. It is possible and simple to delete queues from this interface.

孤独难免 2024-12-02 02:59:11

我进一步概括了 Piotr Stapp 的 JavaScript/jQuery 方法,将其封装到一个函数中并对其进行了概括。

此函数使用 RabbitMQ HTTP API 查询给定 vhost 中的可用队列,然后根据可选的 queuePrefix 删除它们:

function deleteQueues(vhost, queuePrefix) {
    if (vhost === '/') vhost = '%2F';  // html encode forward slashes
    $.ajax({
        url: '/api/queues/'+vhost, 
        success: function(result) {
            $.each(result, function(i, queue) {
                if (queuePrefix && !queue.name.startsWith(queuePrefix)) return true;
                $.ajax({
                    url: '/api/queues/'+vhost+'/'+queue.name, 
                    type: 'DELETE', 
                    success: function(result) { console.log('deleted '+ queue.name)}
                });
            });
        }
    });
};

将此函数粘贴到浏览器的 JavaScript 控制台后在 RabbitMQ 管理页面上,您可以像这样使用它:

删除 '/' vhost 中的所有队列

deleteQueues('/');

删除 '/' vhost 中以 'test' 开头的所有队列

deleteQueues('/', 'test');

删除 'dev' vhost 中以 'test' 开头的所有队列'foo'

deleteQueues('dev', 'foo');

请自行承担使用此功能的风险!

I've generalized Piotr Stapp's JavaScript/jQuery method a bit further, encapsulating it into a function and generalizing it a bit.

This function uses the RabbitMQ HTTP API to query available queues in a given vhost, and then delete them based on an optional queuePrefix:

function deleteQueues(vhost, queuePrefix) {
    if (vhost === '/') vhost = '%2F';  // html encode forward slashes
    $.ajax({
        url: '/api/queues/'+vhost, 
        success: function(result) {
            $.each(result, function(i, queue) {
                if (queuePrefix && !queue.name.startsWith(queuePrefix)) return true;
                $.ajax({
                    url: '/api/queues/'+vhost+'/'+queue.name, 
                    type: 'DELETE', 
                    success: function(result) { console.log('deleted '+ queue.name)}
                });
            });
        }
    });
};

Once you paste this function in your browser's JavaScript console while on your RabbitMQ management page, you can use it like this:

Delete all queues in '/' vhost

deleteQueues('/');

Delete all queues in '/' vhost beginning with 'test'

deleteQueues('/', 'test');

Delete all queues in 'dev' vhost beginning with 'foo'

deleteQueues('dev', 'foo');

Please use this at your own risk!

寄与心 2024-12-02 02:59:11

如果您使用的是 localhost,请安装

$ sudo rabbitmq-plugins enable rabbitmq_management

并转到 http://localhost:15672/#/queues。默认密码为用户名:guest密码:guest
并转到队列选项卡并删除队列。

install

$ sudo rabbitmq-plugins enable rabbitmq_management

and go to http://localhost:15672/#/queues if you are using localhost. the default password will be username: guest, password: guest
and go to queues tab and delete the queue.

浅浅 2024-12-02 02:59:11

管理插件(Web 界面)为您提供了 Python 脚本的链接。您可以使用它来删除队列。我使用这种模式删除了很多队列:

python tmp/rabbitmqadmin --vhost=... --username=... --password=... list queues > tmp/q

vi tmp/q # remove all queues which you want to keep

cut -d' ' -f4 tmp/q| while read q; 
    do python tmp/rabbitmqadmin --vhost=... --username=... --password=... delete queue name=$q; 
done

The management plugin (web interface) gives you a link to a python script. You can use it to delete queues. I used this pattern to remove a lot of queues:

python tmp/rabbitmqadmin --vhost=... --username=... --password=... list queues > tmp/q

vi tmp/q # remove all queues which you want to keep

cut -d' ' -f4 tmp/q| while read q; 
    do python tmp/rabbitmqadmin --vhost=... --username=... --password=... delete queue name=$q; 
done
揪着可爱 2024-12-02 02:59:11

希望它可以帮助某人。

我尝试了上面的代码,但没有进行任何流处理。

sudorabbitmqctllist_queues | sudorabbitmqctllist_queues| awk '{print $1}' >队列.txt;对于 $(catqueues.txt) 中的行;执行 sudorabbitmqctldelete_queue“$line”;完成。

我生成一个包含所有队列名称的文件,并逐行循环遍历该文件以删除它们。对于循环,while read ... 没有为我做到这一点。它总是停在第一个队列名称处。

另外,如果您想删除单个队列,上述解决方案将有所帮助(python,Java ...),并且还可以执行 sudorabbitmqctl delete_queuequeue_name 。我使用的是rabbitmqctl而不是rabbitmqadmin

Hopefully it might help someone.

I tried the above pieces of code but I did not do any streaming.

sudo rabbitmqctl list_queues | awk '{print $1}' > queues.txt; for line in $(cat queues.txt); do sudo rabbitmqctl delete_queue "$line"; done.

I generate a file that contains all the queue names and loops through it line by line to the delete them. For the loops, while read ... did not do it for me. It was always stopping at the first queue name.

Also, if you want to delete a single queue, the above solutions will help(python, Java ...) and also do sudo rabbitmqctl delete_queue queue_name. I am using rabbitmqctl instead of rabbitmqadmin.

雨的味道风的声音 2024-12-02 02:59:11

我正在努力寻找适合我手动删除 rabbigmq 队列需求的答案。因此,我认为在这个线程中值得一提的是,可以使用以下命令在没有rabbitmqadmin的情况下删除单个队列:

rabbitmqctl delete_queue <queue_name>

I was struggling with finding an answer that suited my needs of manually delete a queue in rabbigmq. I therefore think it is worth mentioning in this thread that it is possible to delete a single queue without rabbitmqadmin using the following command:

rabbitmqctl delete_queue <queue_name>
冰火雁神 2024-12-02 02:59:11

我在 .profile 中使用此别名:

alias qclean="rabbitmqctl list_queues | python ~/bin/qclean.py"

其中 qclean.py 具有以下代码:

import sys
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()

queues = sys.stdin.readlines()[1:-1]
for x in queues:
    q = x.split()[0]
    print 'Deleting %s...' %(q)
    channel.queue_delete(queue=q)

connection.close()

本质上,这是 Shweta B. Patil 代码的迭代版本。

I use this alias in .profile:

alias qclean="rabbitmqctl list_queues | python ~/bin/qclean.py"

where qclean.py has the following code:

import sys
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()

queues = sys.stdin.readlines()[1:-1]
for x in queues:
    q = x.split()[0]
    print 'Deleting %s...' %(q)
    channel.queue_delete(queue=q)

connection.close()

Essentially, this is an iterative version of code of Shweta B. Patil.

╰沐子 2024-12-02 02:59:11

安装了rabbitmq_management插件后,您可以运行它来删除所有不需要的队列:

rabbitmqctl list_queues -p vhost_name |\
grep -v "fast\|medium\|slow" |\
tr "[:blank:]" " " |\
cut -d " " -f 1 |\
xargs -I {} curl -i -u guest:guest -H "content-type:application/json" -XDELETE http://localhost:15672/api/queues/<vhost_name>/{}

让我们分解一下命令:

rabbitmqctl list_queues -p vhost_name将列出所有队列以及它们当前有多少任务。

grep -v "fast\|medium\|slow" 将过滤您不想删除的队列,假设我们要删除每个不带单词 fast 的队列、

tr "[:blank:]" " " 将标准化rabbitmqctl上队列名称和任务数量之间的分隔符

cut -d " " -f 1 将用空格分割每一行并选择第一列(队列名称)

xargs -I {}curl -i -u guest:guest -H "content-type:application/ json” -XDELETE http://localhost:15672/api/queues//{} 将获取队列名称并将其设置到我们设置 {} 字符的位置删除在此过程中未过滤的所有队列。

确保所使用的用户具有管理员权限。

With the rabbitmq_management plugin installed you can run this to delete all the unwanted queues:

rabbitmqctl list_queues -p vhost_name |\
grep -v "fast\|medium\|slow" |\
tr "[:blank:]" " " |\
cut -d " " -f 1 |\
xargs -I {} curl -i -u guest:guest -H "content-type:application/json" -XDELETE http://localhost:15672/api/queues/<vhost_name>/{}

Let's break the command down:

rabbitmqctl list_queues -p vhost_name will list all the queues and how many task they have currently.

grep -v "fast\|medium\|slow" will filter the queues you don't want to delete, let's say we want to delete every queue without the words fast, medium or slow.

tr "[:blank:]" " " will normalize the delimiter on rabbitmqctl between the name of the queue and the amount of tasks there are

cut -d " " -f 1 will split each line by the whitespace and pick the 1st column (the queue name)

xargs -I {} curl -i -u guest:guest -H "content-type:application/json" -XDELETE http://localhost:15672/api/queues/<vhost>/{} will pick up the queue name and will set it into where we set the {} character deleting all queues not filtered in the process.

Be sure the user been used has administrator permissions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文