RabbitMQ: Deleting a ghost queue that cannot be removed at the GUI/CLI

If you get a timeout/errors trying to delete a RabbitMQ queue from  the management dashboard or CLI with an error similar in syntax to below:

failed to perform operation on queue '' in vhost '' due to timeout

Then you can attempt deletion using rabbitmqctl to evaluate an Erlang expression:

rabbitmqctl eval 'Q = {resource, <<"vhostID">>, queue, <<"queueName">>}, rabbit_amqqueue:internal_delete(Q)."

Substituting for your own ‘vhostID’ and ‘queueName’ values.  The double brackets and quotes must stay in the syntax above.

Note, the command above is for ghosted queues, if the queues are in a normal state you can simply use:

# either of these work to delete queue
rabbitmqctl --vhost="/" delete_queue 'queueName'
rabbitmqadmin --vhost="/" delete queue name='queueName'

Or delete a crashed queue:

rabbitmqctl eval '{ok,Q} = rabbit_amqqueue:lookup(rabbit_misc:r(<<"vhostID">>,queue,<<"queueName">>)),rabbit_amqqueue:delete_crashed(Q).'

 

REFERENCES

pivotal.com, deleting ghost queues using internal_delete

github, another example using internal_delete

pivotal.io, deleting queue using delete_crashed

rabbitmqctl man page

rabbitmq policy man page

rabbitmqadmin man page

docker hub, rabbitmq images

stackoverflow James Oravec, creating queues and bindings from command line, also has curl examples

codeburst.io Changhui Xu, Get Started with RabbitMQ on Docker

medium.com Francesco Bonizzi, RabbitMQ with Docker on Windows in 30 minutes

stackoverflow delete unused queues by setting TTL and allowing to expire

github, ha-promote-on-shutdown setting might avoid ghost queues

sleeplessbeastie.eu, erlang commands for rabbitmqctl

juniper.net, troubleshooting rmq with node status, conn, bindings, stuck processes

 

NOTES

Docker container of rabbitmq with management dashboard, map dashboard URL to localhost at same ports

# run in background
docker run --rm -it -d --hostname my-rabbit --name my-rabbit -p 15672:15672 -p 5672:5672 rabbitmq:3-management

# tail logs
docker logs -f my-rabbit

# get just container id
docker ps -qf "name=my-rabbit"

Pull up rmq dashboard (user/pass=guest/guest)

http://localhost:15672

ssh into rabbitmq container

docker ps -a
docker exec -it my-rabbit /bin/bash

rabbit version

rabbitmqctl eval 'rabbit_misc:version().'

create queue

rabbitmqadmin declare queue --vhost=/ name=test1 durable=true

get queue info

rabbitmqadmin -f long -d 3 list queues

rabbitmqctl eval 'rabbit_amqqueue:lookup(rabbit_misc:r(<<"/">>,queue,<<"test1">>)).'

create rmq policy, apply to all queues in vhost that begin with ‘test’

rabbitmqctl list_policies

rabbitmqctl set_policy -p '/' mypolicy1 '^test.*' '{"ha-mode":"exactly","ha-params":2,"ha-sync-mode":"automatic"}'

delete queue

rabbitmqadmin list queues
# format that can be used in bash pipeline
rabbitmqadmin -f tsv -q list queues name

rabbitmqadmin delete queue name='queuename'

rabbitmqctl eval '{ok,Q} = rabbit_amqqueue:lookup(rabbit_misc:r(<<"/">>,queue,<<"queuename">>)),rabbit_amqqueue:delete_crashed(Q).'

delete stubborn ghost queue

rabbitmqctl eval 'Q = {resource, <<"vhostID">>, queue, <<"queueName">>}, rabbit_amqqueue:internal_delete(Q)."