Remove Nodes From Docker Swarm

Removing a node from the cluster

Docker swarm leave

To leave from a cluster, run the below command on the node -

$ docker swarm leave

output

root@worker02:~# docker swarm leave
Node left the swarm.
root@worker02:~#
After running this command, worker02 will be detached from the cluster.

now list the node again and see the output:

root@master:~# docker node ls
ID                            HOSTNAME   STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
buc72h0hyo66gcilq7melh9pq *   master     Ready     Active         Leader           20.10.11
xvdyauu8t83r3udrqh6c5w8a8     worker01   Ready     Active                          20.10.11
xmq9x22q50h4qlp9311796hd9     worker02   Down      Active                          20.10.11
root@master:~#

now you can notice that status of worker02 is Down.

To remove one or more nodes from the swarm, use the docker node rm command.

Examples

Remove a down node from the swarm

$ docker node rm worker2

output:

root@master:~# docker node ls
ID                            HOSTNAME   STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
buc72h0hyo66gcilq7melh9pq *   master     Ready     Active         Leader           20.10.11
xvdyauu8t83r3udrqh6c5w8a8     worker01   Ready     Active                          20.10.11
xmq9x22q50h4qlp9311796hd9     worker02   Down      Active                          20.10.11
root@master:~# docker node rm worker02
worker02
root@master:~# docker node ls
ID                            HOSTNAME   STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
buc72h0hyo66gcilq7melh9pq *   master     Ready     Active         Leader           20.10.11
xvdyauu8t83r3udrqh6c5w8a8     worker01   Ready     Active                          20.10.11
root@master:~#

To forcibly remove an inaccessible node from a swarm, use --force option along with the rm command

$ docker node rm --force worker01