Delete all the queues from RabbitMQ?
Asked Answered
C

27

226

I installed rabbitmqadmin and was able to list all the exchanges and queues. How can I use rabbitmqadmin or rabbitmqctl to delete all the queues.

Capybara answered 12/7, 2012 at 19:59 Comment(0)
S
327

First, list your queues:

rabbitmqadmin list queues name

Then from the list, you'll need to manually delete them one by one:

rabbitmqadmin delete queue name='queuename'

Because of the output format, doesn't appear you can grep the response from list queues. Alternatively, if you're just looking for a way to clear everything (read: reset all settings, returning the installation to a default state), use:

rabbitmqctl stop_app
rabbitmqctl reset    # Be sure you really want to do this!
rabbitmqctl start_app
Subaudition answered 12/7, 2012 at 20:19 Comment(11)
to see all pending tasks in rabbitmq: rabbitmqctl list_queues name messages messages_ready \ messages_unacknowledgedCroupier
Be aware that "rabbitmqctl reset" will reset everything back to the "factory settings". Any RabbitMQ users, virtual hosts, etc, that you have created will be blown away.Logarithm
Apologies @smartnut007, I've clarified the second portion of the answer with a disclaimer.Subaudition
just grabbing the empty queues. rabbitmqctl list_queues | grep 0 | awk '{print $1}' | xargs -I qn rabbitmqadmin delete queue name=qnPythagoras
@austin 's receipt works perfectly. Just make sure you have root privilege before you run that.Storyteller
@Pythagoras That will delete all queues with a 0 in the name or the count. Might want to do grep $'\t0' or something.Deity
Is the rabbitmqctl "reset" flavor supposed to be compatible with rabbitmq installed via Homebrew? Didn't seem to work work for me. I ended up using one of the one liner answers.Calomel
@Subaudition i know it's a bit dumb question, but where one should run these commands ??Abner
@ZeeshanAjmal you can run them from whatever shell you want, so long as they're in your PATH.Subaudition
seeing answers below this answer seems like a horrible idea. Why would I want to return my all settings to default just because I want to remove some queues.Cleancut
Thanks! Only rabbitmqctl reset helped me with queues in a 'stopped' status. Before resetting, you can export the Definitions file, and after resetting, import the Definitions file, and all queues will be in a running status.Cabrilla
T
151

Actually super easy with management plugin and policies:

  • Goto Management Console (localhost:15672)

  • Goto Admin tab

  • Goto Policies tab(on the right side)

  • Add Policy

  • Fill Fields

    • Virtual Host: Select
    • Name: Expire All Policies(Delete Later)
    • Pattern: .*
    • Apply to: Queues
    • Definition: expires with value 1 (change type from String to Number)
  • Save

  • Checkout Queues tab again

  • All Queues must be deleted

  • And don't forget to remove policy!!!!!!.

Thoth answered 24/8, 2018 at 10:7 Comment(9)
select "Number" at Definition. Does not work with default ("String")Waterford
Great answer, actually made up my day. If you select "Exchanges and Queues" from the list, you could easily delete both Queues and Exchanges. I wish this could be the accepted answer.Klapp
Very clean solution, without the need to play around the instance SSH.Senzer
Pity nobody explains how to install management plugin and policiesTrunkfish
@MesutA. Thanks a lot. I think it's good to have this link in this article. It might be even better to add it to the answer, as comments might be purged. But I have now at least this infoTrunkfish
@Mesut A. @Waterford Is there any particular reason this does not work for me in 3.8.3? I'm using .*guid.* pattern to delete only exchanges/queues that have guid string in them, and the policy have no effect.Brnaby
@Brnaby Number vs String was for the definition field, not for pattern. An older version of this answer did not contain the information to change the type from string to number.Waterford
@Waterford Number vs String for definition is not the case for underlying RabbitMQ version because they've added validator to value field that does not give you ability to add expires = "1", only expires = 1 (Number). To expand on the problem, what I've noticed after adding this policy, it appeared in the "features" column for target exchange ( "D", "ha-all", "NameOfMyPolicy") once (?).Brnaby
how can i delete all the exchanges at once ?Brumbaugh
C
67

With rabbitmqadmin you can remove them with this one-liner:

rabbitmqadmin -f tsv -q list queues name | while read queue; do rabbitmqadmin -q delete queue name=${queue}; done
Cairistiona answered 2/10, 2014 at 22:29 Comment(2)
In my case queues are prefixed with keyword by which I can simply use egrep, so my command will look like this: rabbitmqadmin -f tsv -q list queues name | egrep "%search word%" | while read queue; do rabbitmqadmin -q delete queue name=${queue}; donePhotomicroscope
You may have to use -H to specify host and -u and -p parameters to specify the credentials to connect to serverTyburn
C
33

In Rabbit version 3.7.10 you can run below command with root permission:

rabbitmqctl list_queues | awk '{ print $1 }' | xargs -L1 rabbitmqctl delete_queue
Cleaner answered 4/2, 2019 at 16:11 Comment(2)
Hmm, I have ran it on Unix based OS and it works successfully, just make sure the result that passed to xargs command is ok.Cleaner
for alpine, if you are experiencing unrecognized option: L, install findutils package - it will get you a GNU version of xargsScuta
T
22

Try this:

 rabbitmqadmin list queues name | awk '{print $2}' | xargs -I qn rabbitmqadmin delete queue name=qn
Trista answered 18/2, 2015 at 10:12 Comment(2)
This worked for me, but also showed *** Not found: /api/queues/%2F/name because the output is a ASCII table with a "name" column. I tweaked the command to be rabbitmqadmin list queues name | awk '!/--|name/ {print $2}' | xargs -I qn rabbitmqadmin delete queue name=qn to fix it.Calomel
rabbitmqadmin list queues name | awk {'print$2'} | egrep [^name] | xargs -I qname rabbitmqadmin delete queue name=qnameCupronickel
S
15

If you don't have rabbitmqadmin installed, try to purge queues with rabbitmqctl:

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

Sixpack answered 27/3, 2017 at 11:46 Comment(4)
There is no delete_queue nor purge_queue commands in rabbitmqctl. I would like to purge a lot of queues that seem to be automatically generated and I would not like to install extra software like rabbitmqadmin...Imphal
rabbitmqctl purge_queue worked here manually. I only needed to add -p <virtual-host>Titoism
Contrary to what @Imphal stated above, both delete_queue and purge_queue are available in rabbitmqctl and I've just run them successfully. Perhaps you're on an old version.Canonicate
Good to hear that, these could have been added recently.Imphal
F
13

If you're trying to delete queues because they're unused and you don't want to reset, one option is to set the queue TTL very low via a policy, wait for the queues to be auto-deleted once the TTL is passed and then remove the policy (https://www.rabbitmq.com/ttl.html).

rabbitmqctl.bat set_policy delq ".*" '{"expires": 1}' --apply-to queues

To remove the policy

rabbitmqctl clear_policy delq

Note that this only works for unused queues

Original info here: http://rabbitmq.1065348.n5.nabble.com/Deleting-all-queues-in-rabbitmq-td30933.html

Flunkey answered 12/4, 2016 at 13:52 Comment(2)
this is the fastest waySnide
Definitely the best wayAmicable
D
6

I made a deleteRabbitMqQs.sh, which accepts arguments to search the list of queues for, selecting only ones matching the pattern you want. If you offer no arguments, it will delete them all! It shows you the list of queues its about to delete, letting you quit before doing anything destructive.

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"

If you want different matching against the arguments you pass in, you can alter the grep in line four. When deleting all queues, it won't delete ones with three consecutive spaces in them, because I figured that eventuality would be rarer than people who have rabbitmqctl printing its output out in different languages.

Enjoy!

Draconian answered 13/9, 2013 at 12:6 Comment(0)
C
6

Here is a way to do it with PowerShell. the URL may need to be updated

$cred = Get-Credential
 iwr -ContentType 'application/json' -Method Get -Credential $cred   'http://localhost:15672/api/queues' | % { 
    ConvertFrom-Json  $_.Content } | % { $_ } | ? { $_.messages -gt 0} | % {
    iwr  -method DELETE -Credential $cred  -uri  $("http://localhost:15672/api/queues/{0}/{1}" -f  [System.Web.HttpUtility]::UrlEncode($_.vhost),  $_.name)
 }
Commendam answered 20/11, 2014 at 2:23 Comment(1)
Note, this only deletes non-empty queues. Remove the -gt clause to delete all queuesCheney
G
4

You can use rabbitmqctl eval as below:

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 ].' 

The above will delete all empty queues in all vhosts that have a name beginning with "prefix-". You can edit the variables IfUnused, IfEmpty, and MatchRegex as per your requirement.

Gilstrap answered 19/8, 2016 at 10:59 Comment(4)
Perfect for when rabbitmqadmin is not accessible.Wrens
I found this much faster than list_queuesStorms
Has anyone tried this solution with RabbitMQ v3.8.2 or higher? I seem to be running into some undefined Erlang error. Maybe the solution needs to be updated to reflect newer versions?Precautious
I tried similar command as above but get a syntax error before ^ Below is my command. kubectl exec -n kayaks svc/rabbitmq-ha -- rabbitmqctl --vhost=AM-Dev eval 'IfUnused = false, IfEmpty = true, MatchRegex = <<"^WOMSProvisioningSubscrptionQueue_platform-">>, [rabbit_amqqueue:delete(Q, IfUnused, IfEmpty) || Q <- rabbit_amqqueue:list(), re:run(element(4, element(2, Q)), MatchRegex) =/= nomatch ].'Emmenagogue
F
3

Removing all queues using rabbitmqctl one liner

rabbitmqctl list_queues | awk '{ print $1 }' | sed 's/Listing//' | xargs -L1 rabbitmqctl purge_queue
Fermentation answered 31/1, 2018 at 5:0 Comment(0)
M
3

I tried rabbitmqctl and reset commands but they are very slow.

This is the fastest way I found (replace your username and password):

#!/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/@

Note: This only works with the default vhost /

Monde answered 25/4, 2018 at 14:50 Comment(1)
Thanks! Way better way than most other suggestionsMisappropriate
P
2

You need not reset rabbitmq server to delete non-durable queues. Simply stop the server and start again and it will remove all the non-durable queues available.

Photoflood answered 29/10, 2015 at 9:23 Comment(3)
including durable queues? I don't think so. I'll qualify your answer.Apices
No, durable queues cannot be deleted by stopping the server. They can be deleted from RabbitMQ Management web interface under queues.Photoflood
Actually yes, this helped me and all about 4500 automatically generated queues are gone. It seems that these were non-durable ones. Thanks!Imphal
V
2

In case you only want to purge the queues which are not empty (a lot faster):

rabbitmqctl list_queues | awk '$2!=0 { print $1 }' | sed 's/Listing//' | xargs -L1 rabbitmqctl purge_queue

For me, it takes 2-3 seconds to purge a queue (both empty and non-empty ones), so iterating through 50 queues is such a pain while I just need to purge 10 of them (40/50 are empty).

Vshaped answered 30/3, 2018 at 15:21 Comment(0)
C
2

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.

Cthrine answered 23/4, 2019 at 14:21 Comment(0)
H
2

For whose have a problem with installing rabbitmqadmin, You should firstly install python.

UNIX-like operating system users need to copy rabbitmqadmin to a directory in PATH, e.g. /usr/local/bin.

Windows users will need to ensure Python is on their PATH, and invoke rabbitmqadmin as python.exe rabbitmqadmin.

Then

  1. Browse to http://{hostname}:15672/cli/rabbitmqadmin to download.
  2. Go to the containing folder then run cmd with administrator privilege

To list Queues python rabbitmqadmin list queues.

To delete Queue python rabbitmqadmin delete queue name=Name_of_queue

To Delete all Queues

1- Declare Policy

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

2- Remove the policy

python rabbitmqadmin  delete policy name='expire_all_policies'
Heyes answered 14/11, 2019 at 7:47 Comment(0)
B
1

Here is a faster version (using parallel install sudo apt-get install parallel) expanding on the excellent answer by @admenva

parallel -j 50 rabbitmqadmin -H YOUR_HOST_OR_LOCALHOST -q delete queue name={} ::: $(rabbitmqadmin -H YOUR_HOST_OR_LOCALHOST -f tsv -q list queues name)

Barbirolli answered 15/3, 2015 at 15:45 Comment(0)
T
1

This commands deletes all your queues

python rabbitmqadmin.py \
  -H YOURHOST -u guest -p guest -f bash list queues | \
xargs -n1 | \
xargs -I{} \
  python rabbitmqadmin.py -H YOURHOST -u guest -p guest delete queue name={}

This script is super simple because it uses -f bash, which outputs the queues as a list.

Then we use xargs -n1 to split that up into multiple variables

Then we use xargs -I{} that will run the command following, and replace {} in the command.

Ty answered 18/9, 2015 at 9:26 Comment(2)
I've tried like 10 different answers, and this is the ONLY thing that has actually worked to delete queues without killing all my other settings. Thanks! I can't believe rabbitmqctl doesn't just have a "drop all queues" command.Trilogy
BTW, to get rabbitmqadmin, you need to go to http://yourhost:15672/cli/ and download it.Trilogy
B
1

To list queues,

./rabbitmqadmin -f tsv -q list queues

To delete a queue,

./rabbitmqadmin delete queue name=name_of_queue
Brasier answered 19/11, 2018 at 16:41 Comment(0)
V
1

Following command worked for me:

sudo rabbitmqctl list_queues | awk '{print $1}' | xargs -I qn sudo rabbitmqctl delete_queue qn

Veroniqueverras answered 9/6, 2022 at 6:25 Comment(0)
M
0

There's a way to remove all queues and exchanges without scripts and full reset. You can just delete and re-create a virtual host from admin interface. This will work even for vhost /.

The only thing you'll need to restore is permissions for the newly created vhost.

Mani answered 11/11, 2015 at 9:47 Comment(0)
C
0

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

Constipate answered 12/1, 2018 at 18:40 Comment(0)
T
0

Another option is to delete the vhost associated with the queues. This will delete everything associated with the vhost, so be warned, but it is easy and fast.

Tiling answered 22/12, 2018 at 16:22 Comment(0)
H
0

This is a method I use. It is easy, clear and effective. This is the document:

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 
Harvin answered 17/1, 2022 at 8:20 Comment(0)
U
0

Try this:

rabbitmqctl list_queues -q name > q.txt
IFS=$'\n' read -d '' -r -a queues < q.txt
count=${#queues[@]}
i=1; while (($i < $count)); do echo ${queues[$i]};rabbitmqctl delete_queue ${queues[$i]};i=$((i+1)); done
Unsteel answered 21/7, 2022 at 11:9 Comment(1)
Your answer could be improved by adding more information on what the code does and how it helps the OP.Eduction
M
0

As per https://mcmap.net/q/117842/-delete-all-the-queues-from-rabbitmq

To automate that, it's possible to use this 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'

Please note that %2f is default vhost name (/) and guest:guest is login:password

Maltese answered 3/9, 2022 at 9:21 Comment(0)
K
-1
rabbitmqadmin list queues|awk 'NR>3{print $4}'|head -n-1|xargs -I qname rabbitmqadmin delete queue name=qname
Krasnoyarsk answered 20/2, 2014 at 11:22 Comment(2)
I receive this when running it: head: illegal line count -- -1Quincy
The "head -n-1" should be either "head -1" or "head -n 1"Dozy

© 2022 - 2024 — McMap. All rights reserved.