'How to remove non-running nodes from RabbitMQ cluster

I need to delete all nodes that are not running in a RabbitMQ cluster via the command line.

I have tried rabbitmqctl forget_cluster_node, but I'm not sure how to get the list of non-running nodes.

I see all the nodes and running_nodes in the output of rabbitmqctl cluster_status. Can someone help me parse it and let me know if there is any other solution to delete the nodes from a cluster easily?



Solution 1:[1]

Figured out by myself

# Remove nodes that are not running from the cluster
nodes=($(egrep -o '[a-z0-9@-]+' <<< $(sudo rabbitmqctl cluster_status --formatter json | jq .nodes.disc)))
running_nodes=($(egrep -o '[a-z0-9@-]+' <<< $(sudo rabbitmqctl cluster_status --formatter json | jq .running_nodes)))

for node in ${nodes[@]}
do
  match_count=0
  for rnode in ${running_nodes[@]}
  do
    if [ "${node}" == "${rnode}" ]
    then
        match_count=1
        break
    fi
  done
  if [ $match_count == 1 ]
    then
        continue
    else
      sudo rabbitmqctl forget_cluster_node $node
    fi
done

Solution 2:[2]

In my case the proposed script didn't worked, possibly due RabbitMQ version output differs on the version I'm using(3.9.13). Anyhow, this is what I ended up using:

#!/bin/bash
offline_nodes=$(rabbitmqctl --quiet --formatter json cluster_status \
  | jq -r '.disk_nodes-.running_nodes | .[]')

for node in ${offline_nodes[@]}; do
  rabbitmqctl forget_cluster_node "$node"
done

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Pang
Solution 2