Premium Hosted Website & Server Monitoring Tool.

(Sysadmin / Devops blog)

visit our website

Blog   >   RabbitMQ   >   Clearing / purging RabbitMQ queues

Clearing / purging RabbitMQ queues

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:

import pika
host = 'rabbitmq1'
creds = pika.PlainCredentials('username', 'password')
params = pika.ConnectionParameters(host, credentials=creds, heartbeat=300)
conn = pika.AsyncoreConnection(params)
ch = conn.channel()
ch.queue_delete(queue='queuename')
ch.close()
conn.close()

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.

  • http://www.rabbitmq.com/ Simon MacMullen

    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.

    • http://gravatar.com/siddharthsaha Siddharth Saha

      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?

      • http://gravatar.com/simonmacmullen Simon MacMullen

        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”.

      • http://gravatar.com/siddharthsaha siddharthsaha

        Thanks Simon. It worked for me.

  • http://gravatar.com/siddharthsaha Siddharth Saha

    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!)

      • http://gravatar.com/simonmacmullen Simon MacMullen

        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”)