There is only one truth. It is the source.

How to purge messages from a RabbitMQ queue

May 25, 2011

Tags: celery, django, rabbitmq

RabbitMQ has no command (as of May 25, 2011) to purge messages from a queue. This doesn't matter if the queue is not durable because you can simply restart the server and the messages will be purged.

If you use a durable queue like Celery does, then the restart trick will not work. You must purge the queue from the programming interface. The following Django management does this:

from amqplib import client_0_8 as amqp
from django.core import management


class Command(management.base.BaseCommand):
    args = 'no arguments'
    help = 'Purges the messages of the "celery" queue of RabbitMQ.'

    def handle(self, *args, **options):
        conn = amqp.Connection(host="localhost:5672 ", userid="guest", password="guest", virtual_host="/")
        channel = conn.channel()
        channel.queue_purge('celery')
        channel.close()
        conn.close()

        self.stdout.write('Successfully purged the celery queue\n')