FAQ: How to close all connections in RabbitMQ?

Frequently Asked RabbitMQ Question: How do we close all connections in RabbitMQ? This article explains how to close a single or multiple connections in RabbitMQ.
RabbitMQ Frequently Asked Questions

There are different options of how to close connections in RabbitMQ. The web based UI can be used via the RabbitMQ Management Interface, a scripts can be used via rabbitmqadmin or HTTP API curl. A script is recommended if you need to close multiple connections.

Close connections via:

RabbitMQ Management Interface

A connection can be closed via the RabbitMQ Management Interface. Enter the connection tab and press on the connection. Go to the bottom of the page and press Close this connection, followed by pressing Force Close.

Close RabbitMQ connection via the mgmt interface

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

Close one connection:

$ rabbitmqadmin -q close connection name=name_of_connection

In CloudAMQP the management plugin is assigned port 443 and the ssl flag has to be used, as shown below.

$ rabbitmqadmin --host=HOST --port=443 --ssl --vhost=VHOST --username=USERNAME --password=PASSWORD -q close connection name=name_of_connection
Close RabbitMQ connection via rabbitmq admin

Close all connections

The script below will:

  1. Add all connections into a file called c.txt. You can open the file and remove the connections from the file that you would like to keep.
  2. Loop the list of connections and for each connection close it.
$ rabbitmqadmin -f tsv -q list connections name > c.txt

$ while read -r name; do rabbitmqadmin -q close connection name="${name}"; done < c.txt

In CloudAMQP the management plugin is assigned port 443 and the ssl flag has to be used, as shown below.

$ rabbitmqadmin --host=HOST --port=443 --ssl --vhost=VHOST --username=USERNAME --password=PASSWORD -f tsv -q list connections name > c.txt

$ while read -r name; do rabbitmqadmin -q --host=HOST --port=443 --ssl --vhost=VHOST --username=USERNAME --password=PASSWORD -q close connection name="${name}"; done < c.txt
close all connections rabbitmq

HTTP API, curl

The RabbitMQ Management plugin provides an HTTP-based API for management and monitoring of your RabbitMQ server. In CloudAMQP the management plugin is assigned port 443 and SSL has to be used.

curl -i -XDELETE https://USERNAME:PASSWORD@HOST/api/connections/VHOST/CONNECTION_NAME

rabbitmqctl

The RabbitMQ CLI provides a way (since version 3.7.0) to close all connections for a vhost, or even the whole server. See all arguments in the rabbitmqctl documentation

rabbitmqctl close_all_connections --vhost $VHOST "Closed by request"

Please email us at contact@cloudamqp.com if you have any suggestions, questions or feedback.

CloudAMQP - industry leading RabbitMQ as a service

Start your managed cluster today. CloudAMQP is 100% free to try.

13,000+ users including these smart companies