174

How do I delete all messages from a single queue using the cli? I have the queue name and I want to clean it.

tread
  • 10,133
  • 17
  • 95
  • 170
Bick
  • 17,833
  • 52
  • 146
  • 251

11 Answers11

189

you can directly run this command

sudo rabbitmqctl purge_queue queue_name
Anurag jain
  • 2,281
  • 1
  • 11
  • 10
  • 18
    or, if you have a virtual host, do `rabbitmqctl purge_queue queue_name -p my_virt_host` – goat Jun 01 '16 at 15:49
  • 4
    run sudo rabbitmqctl -h and check list of cammand listed by your current rabbitmq. if it is not there it means current version of rabbitmq does not support this feature. – Anurag jain Jun 07 '16 at 06:28
  • 1
    This didn't work for me -- as soon as my consumer fired up, the queue was still full of tasks. – MuffintopBikini Aug 29 '17 at 22:58
  • This could have been implemented in either 3.5.4 or 3.6.0, based on https://github.com/rabbitmq/rabbitmq-server/pull/215 and https://www.rabbitmq.com/changelog.html . If you have an older version, rabbitmqadmin as per https://stackoverflow.com/a/18267342/272387 might help. – Richlv Oct 25 '17 at 11:44
  • 1
    ...and checking in https://github.com/rabbitmq/rabbitmq-server/releases/tag/rabbitmq_v3_5_4 , this feature appeared in 3.5.4. – Richlv Oct 26 '17 at 13:25
142

rabbitmqadmin is the perfect tool for this

rabbitmqadmin purge queue name=name_of_the_queue_to_be_purged
pr4n
  • 2,918
  • 3
  • 30
  • 42
  • 5
    I had a hard time finding the tool in my rabbitmq installation. I finally downloaded it from here: http://hg.rabbitmq.com/rabbitmq-management/raw-file/rabbitmq_v3_3_5/bin/rabbitmqadmin – Mukul M. Aug 30 '14 at 13:21
  • 5
    If you have the management plugin already installed, you can downloaded it from `http://rabbitserver:15672/cli/` – morloch Jul 24 '15 at 08:13
  • Also to list available queues try rabbitmqctl list_queues – Ramast May 18 '23 at 18:29
26

RabbitMQ has 2 things under queue

  1. Delete
  2. Purge

Delete - will delete the queue

Purge - This will empty the queue (meaning removes messages from the queue but queue still exists)

Guru Prasad
  • 569
  • 6
  • 13
17

To purge queue you can use following command (more information in API doc):

curl -i -u guest:guest -XDELETE http://localhost:15672/api/queues/vhost_name/queue_name/contents
mpromonet
  • 11,326
  • 43
  • 62
  • 91
Olga
  • 171
  • 1
  • 2
  • Kind of scary how well this worked. Any way to protect against this happening unintentionally? Such as a config for production servers that disables this functionality to prevent accidental data loss? – Isaiah May 07 '18 at 23:06
  • 1
    Yeah, the protection would be to delete the user "guest" using the "delete_user" command – Ocean Airdrop Oct 30 '18 at 16:00
16

RabbitMQ implements the Advanced Message Queuing Protocol (AMQP) so you can use generic tools for stuff like this.

On Debian/Ubuntu or similar system, do:

sudo apt-get install amqp-tools
amqp-delete-queue -q celery  # where celery is the name of the queue to delete
amqp-declare-queue -d -q celery # where celery is the name of the queue to delete and the "-d" creates a durable/persistent queue

Edit 2022: Added amqp-declare-queue to the example

benjaoming
  • 2,135
  • 1
  • 21
  • 29
  • That's a good solution for some version do not have rabbitmqadmin. – WisZhou Mar 23 '16 at 10:06
  • 13
    This deletes the whole queue, doesn't just purge it. So the queue doesn't exist anymore and you have to re-initialize the empty queue afterwards. – krob Jun 30 '16 at 18:22
  • That's true. Luckily, ampq-tools also has a command to create a queue. The intention of this Answer was to show how we can achieve what the question asks using generic tools or when getting rabbitmqadmin isn't feasible. – benjaoming Oct 12 '22 at 13:13
8

IMPORTANT NOTE: This will delete all users and config.

ALERT !!

ALERT !!

I don't suggest this answer until unless you want to delete data from all of the queues, including users and configs. Just Reset it !!!

rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl start_app
naren
  • 14,611
  • 5
  • 38
  • 45
  • 31
    That also resets your users and other configs! – Codewithcheese Feb 27 '15 at 13:04
  • I used this as part of unit tests. e.g. I clear everything, then set it all up programmically via http://stackoverflow.com/questions/4545660/rabbitmq-creating-queues-and-bindings-from-a-command-line/25915568#25915568 then populate some messages and do black box testing to make sure messages went through. Works great for this purpose. :) – James Oravec Jun 30 '15 at 16:25
  • 4
    zeroing your storage and reinstalling the OS also gets rid of the data; this is not what OP is asking – istepaniuk Jun 05 '19 at 14:58
6

In order to delete only messages from the queue use :

sudo rabbitmqctl --node <nodename> purge_queue <queue_name>

In order to delete a queue which is empty(--if-empty) or has no consumers(--if-unused) use :

sudo rabbitmqctl --node <nodename> delete_queue <queue_name> --if-empty

or

sudo rabbitmqctl --node <nodename> delete_queue <queue_name> --if-unused 
Ashwani Singh
  • 876
  • 11
  • 10
5

I guess its late but for others reference, this can be done with pika

import pika
host_ip = #host ip
channel = pika.BlockingConnection(pika.ConnectionParameters(host_ip,
                                                        5672,
                                                        "/",
credentials=pika.PlainCredentials("username","pwd"))).channel()
print "deleting queue..", channel.queue_delete(queue=queue_name)
prajnavantha
  • 1,111
  • 13
  • 17
4

In case you are using RabbitMQ with Docker your steps should be:

  1. Connect to container: docker exec -it your_container_id bash
  2. rabbitmqctl purge_queue Queue-1 (where Queue-1 is queue name)
Roman Motovilov
  • 344
  • 3
  • 5
  • Why would it be different if one would purge the queue from outside the docker container? Isn't the queue completely oblivious to **whom** (or rather **what**) is using it? I am not suggesting anything, I am just asking. – M.Ionut Aug 25 '21 at 07:22
3

I have successfully used ampq-purge from amqp-utils to do this:

git clone https://github.com/dougbarth/amqp-utils.git
cd amqp-utils
# extracted from Rakefile
echo "source 'https://rubygems.org'
gem 'amqp', '~> 0.7.1'
gem 'trollop', '~> 1.16.2'
gem 'facets', '~> 2.9'
gem 'clio', '~> 0.3.0'
gem 'json', '~> 1.5'
gem 'heredoc_unindent', '~> 1.1.2'
gem 'msgpack', '~> 0.4.5'" > Gemfile
bundle install --path=$PWD/gems
export RUBYLIB=.
export GEM_HOME=$PWD/gems/ruby/1.9.1

ruby bin/amqp-purge -v -V /vhost -u user -p queue
# paste password at prompt
andrewdotn
  • 32,721
  • 10
  • 101
  • 130
1

My rabbitmqclt was an older version without purge_queue, and I did not have rabbitmqadmin installed.

Our app runs on celery and it has this command that worked:

celery -A <app_name> -Q <queue_name> purge
Asmo Soinio
  • 1,182
  • 12
  • 12