Clearing / purging RabbitMQ queues
Written by David Mytton — Subscribe now.
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.