Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Purge a queue in RabbitMQ

Tags:

c#

rabbitmq

I am using RabbitMQ for daily transactions. My consumers are .Net desktop applications deployed in multiple machines. Every day the transactions are pushed to queue within a certain time duration only. Beyond that there needs to be a hard stop on any new transaction. I have managed to stop sending new transaction to the queue. However, the existing transactions in the queue also needs to be flushed so that it is not sent to any consumer. I tried searching for this but did not get any solution for purging a queue except for two options-

  • Delete and re-create the queue every day
  • Stop all the consumers of the queue

Both of these approaches can be implemented but it requires significant amount of changes on my systems. I want to know if there is a better approach.

like image 982
Display name Avatar asked Jan 30 '17 10:01

Display name


People also ask

How do I clear my queue?

In the navigation pane, choose Queues. On the Queues page, choose the queue to delete. Choose Delete. In the Delete queue dialog box, confirm the deletion by entering delete .

How do you purge Unacked messages in RabbitMQ?

Now go back to the Queues tab. Click on the queue with the 'Unacked' message. Expand the Purge section and hit the Purge Messages button.

What is auto delete queue RabbitMQ?

An auto-delete queue will be deleted when its last consumer is cancelled (e.g. using the basic. cancel in AMQP 0-9-1) or gone (closed channel or connection, or lost TCP connection with the server). If a queue never had any consumers, for instance, when all consumption happens using the basic.

How do I reset RabbitMQ?

To reset a running and responsive node, first stop RabbitMQ on it using rabbitmqctl stop_app and then reset it using rabbitmqctl reset: # on rabbit1 rabbitmqctl stop_app # => Stopping node rabbit@rabbit1 ... done.


2 Answers

In C# you can purge a Queue like this

            ConnectionFactory factory = new ConnectionFactory();

            factory.HostName = "localhost";
            factory.UserName = "guest";
            factory.Password = "guest";

            using (var connection = factory.CreateConnection())
            {
                using (var channel = connection.CreateModel())
                {
                    channel.QueuePurge(queueName);
                }

            }
like image 117
Moiz Avatar answered Sep 21 '22 19:09

Moiz


This blog article describes how to purge a queue in RabbitMQ in different ways.

rabbitmqadmin: The management plugin ships with a command line tool, rabbitmqadmin, which can perform the same actions as the web-based UI (the RabbitMQ management interface).

The script used to purge all messages in a single queue is:

$ rabbitmqadmin purge queue name=name_of_queue

HTTP API: The Rabbitmq Management plugin provides an HTTP-based API for management and monitoring of your RabbitMQ server.

curl -i -XDELETE https://USERNAME:PASSWORD@HOST/api/queues/vhost/QUEUE_NAME/contents

Policy: Add a policy that matches the queue names with an max-lenght rule. A policy can be added by entering the Management Interface and then pressing the admin tab. (Don't forget to delete the policy after it has been applied.)

like image 44
Lovisa Johansson Avatar answered Sep 20 '22 19:09

Lovisa Johansson