Tuesday, April 6, 2010

RabbitMQ purge a queue

Such a simple operation is not available in rabbitmqctl. You can list the queues but not clear it so wrote a python client for it. Better solution is to install BQL plugin but for now this would suffice

import sys
from amqplib import client_0_8 as amqp
if __name__ == '__main__':
    if len(sys.argv) < 6:
       print "Usage python purge_queue.py mq_url mq_user mq_pass mq_vhost queue_name"
       exit()
  
    mq_url=sys.argv[1]
    mq_user=sys.argv[2]
    mq_pass=sys.argv[3]
    mq_vhost=sys.argv[4]
    mq_queue_name=sys.argv[5]
    conn = amqp.Connection(host=mq_url,
                           userid=mq_user,
                           password=mq_pass,
                           virtual_host=mq_vhost,
                           insist=False);
    chan = conn.channel();
    n=chan.queue_purge(mq_queue_name);   
    if n==0:
        print "purged %s sucessfully" % mq_queue_name
    else:
        print "unable to purge %s. There are still %s messages in queue" % (mq_queue_name, n)
    chan.close();
    conn.close();

1 comment: