Clearing / purging RabbitMQ queues

By David Mytton,
CEO & Founder of Server Density.

Published on the 21st October, 2010.

We use RabbitMQ as part of our alert processing backend for our server monitoring service, Server Density. Earlier this week we had a problem with one of our queues suddenly getting very large which caused RabbitMQ to exceed its memory limit. When this happened it prevented RabbitMQ from accepting any new connections.

Clearing the RabbitMQ queue with Python

The fix for this problem was to clear the queue in question but there is currently no mechanism to do this in RabbitMQ without connecting to it directly via AMQP. Normally, we would use a small Python script using the Pika AMQP module to connect and purge the queue:

[sourcecode language=”python”]import pika
host = ‘rabbitmq1’
creds = pika.PlainCredentials(‘username’, ‘password’)
params = pika.ConnectionParameters(host, credentials=creds, heartbeat=300)
conn = pika.AsyncoreConnection(params)
ch =

Of course this wasn’t possible because the memory issue was preventing any connections. The rabbitmqctl admin tool doesn’t have any functionality to do this and there doesn’t appear to be any method other than via AMQP.

Removing the data files

Without any method to delete the queue, the only alternative is to stop RabbitMQ, remove the data files and then restart it. We’re using durable queues so restarting the server without removing the data files would make no difference. This was a case of moving the files from /var/lib/rabbitmq/mnesia/rabbit@rabbit1 elsewhere so that RabbitMQ couldn’t find them. This resolved the problem.

  • Note: as of release 2.1.1 (err, two whole days ago!) the RabbitMQ management plugin lets you purge queues, via web GUI, command line tool or HTTP request.

    • Simon – Would you tell us how to delete and/or purge queues using rabbitmqctl. I did not find anything in the man page to do that?

      • Hi Siddharth. rabbitmqctl doesn’t let you purge queues (it’s not part of the management plugin). If you have the management plugin installed you can use its command line tool – rabbitmqadmin – to delete or purge queues, see “rabbitmqadmin –help”.

      • Thanks Simon. It worked for me.

  • Simon – Would you tell us how to delete and/or purge queues using rabbitmqctl. I did not find anything in the man page to do that?

    • BeeMan

      Run these 3 and you’re done:

      rabbitmqctl stop_app
      rabbitmqctl reset
      rabbitmqctl stop_app

      (Do not stop the rabbitmq-server service itself!)

      • This will delete *everything* in the broker, I think Siddharth was looking for a way to purge individual queues.

        (Also, the third command should be “rabbitmqctl start_app”)

  • Gerb Sterrenburg

    inside that ‘/var/lib/rabbitmq/mnesia/rabbit@rabbit1’ directory there is a ‘queues’ directory, clearing out that one will do the trick as well.

Articles you care about. Delivered.

Help us speak your language. What is your primary tech stack?

Maybe another time