Disconnect all clients

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.

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

  2. From the “API Access” page, copy the “REST API BASE URL”

  3. Use the URL and the token to get the list of all clients:

    curl -H 'Authorization: Bearer <YOUR TOKEN HERE>' <YOUR URL HERE>/mqtt/clients
  4. If 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"}
  5. To extract the ID, use the jq command

    echo '{"ts": "2025-05-27T11:05:48+00:00", "id": "Alice"}' | jq -r '.id'

    This should print Alice.

  6. 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>
  7. 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" } } }
  8. To extract the sessionExpiryInterval, use the jq command.

    echo '{ "client": { "id": "Alice", "connected": false, "sessionExpiryInterval": 4294967295, "messageQueueSize": 0, "willPresent": false, "restrictions": { "maxQueueSize": 200000, "queuedMessageStrategy": "DISCARD_OLDEST" } } }' | jq -r '.client.sessionExpiryInterval'
  9. To disconnect the client’s session from the server, use the DELETE request

    curl -X DELETE -H 'Authorization: Bearer <YOUR TOKEN HERE>' <YOUR URL HERE>/mqtt/clients/<YOUR CLIENT ID>
  10. 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.

 

Related articles