Disconnect all clients
Problem
I have created many sessions with a very long session expiry interval, 4294967295 seconds (136 years). I would like to disconnect these old clients.
Solution
If you are an owner of a HiveMQ Cloud Starter or Enterprise cluster, you can use the HiveMQ Cloud Rest API.
Create API Token. To achieve that, log in to HiveMQ Cloud Console https://console.hivemq.cloud/ click on your target cluster, go to the menu “API Access” and generate a new full access token. Important: a full access token is required.
From the “API Access” page, copy the “REST API BASE URL”
Use the URL and the token to get the list of all clients:
curl -H 'Authorization: Bearer <YOUR TOKEN HERE>' <YOUR URL HERE>/mqtt/clientsIf everything works, this returns the list of clients:
{"ts": "2025-05-27T11:05:48+00:00", "id": "Alice"} {"ts": "2025-05-27T11:05:48+00:00", "id": "Bob"} {"ts": "2025-05-27T11:05:48+00:00", "id": "Verena"}To extract the ID, use the
jqcommandecho '{"ts": "2025-05-27T11:05:48+00:00", "id": "Alice"}' | jq -r '.id'This should print
Alice.Use the URL and the token to get the details of the client.
curl -H 'Authorization: Bearer <YOUR TOKEN HERE>' <YOUR URL HERE>/mqtt/clients/<YOUR CLIENT ID>If everything works, this returns the client details.
{ "client": { "id": "Alice", "connected": false, "sessionExpiryInterval": 4294967295, "messageQueueSize": 0, "willPresent": false, "restrictions": { "maxQueueSize": 200000, "queuedMessageStrategy": "DISCARD_OLDEST" } } }To extract the
sessionExpiryInterval, use thejqcommand.echo '{ "client": { "id": "Alice", "connected": false, "sessionExpiryInterval": 4294967295, "messageQueueSize": 0, "willPresent": false, "restrictions": { "maxQueueSize": 200000, "queuedMessageStrategy": "DISCARD_OLDEST" } } }' | jq -r '.client.sessionExpiryInterval'To disconnect the client’s session from the server, use the
DELETErequestcurl -X DELETE -H 'Authorization: Bearer <YOUR TOKEN HERE>' <YOUR URL HERE>/mqtt/clients/<YOUR CLIENT ID>Use the URL and token to attempt to get the client details again
curl -H 'Authorization: Bearer <YOUR TOKEN HERE>' <YOUR URL HERE>/mqtt/clients/<YOUR CLIENT ID>If this returns 404 – success, the client’s session is deleted.