How to delete a detached tmux session?

26

7

I detached myself from a tmux session:

$ tmux ls
0: 1 windows (created Thu Aug 22 22:52:17 2013) [218x59]

Is there anyway I can simply delete it now that I am detached from it?

user784637

Posted 2013-08-23T19:31:54.003

Reputation: 1 365

Related: If you're still attached to a tmux session, you can hit C-d (control + D) to detach from it and delete it in one fell swoop. (Assuming you're at your shell prompt.) – stalepretzel – 2014-05-02T06:03:52.873

Answers

42

You want to use tmux kill-session:

<~> $ tmux ls
0: 1 windows (created Sat Aug 17 00:03:56 2013) [80x23]
2: 1 windows (created Sat Aug 24 16:47:58 2013) [120x34]

<~> $ tmux kill-session -t 2

<~> $ tmux ls
0: 1 windows (created Sat Aug 17 00:03:56 2013) [80x23]

Joe Casadonte

Posted 2013-08-23T19:31:54.003

Reputation: 3 945

2

If you want to delete all the detached sessions you can use the following code:

tmux list-sessions | grep -E -v '\(attached\)$' | while IFS='\n' read line; do
    tmux kill-session -t "${line%%:*}"
done

This solution is more robust than the one proposed by abieler because grep -E -v '\(attached\)$' matches only the detached sessions (the solution by abieler would skip a detached session called attached).

Mateusz Piotrowski

Posted 2013-08-23T19:31:54.003

Reputation: 2 768

1

If you want to kill all detached sessions

tmux list-sessions | grep -v attached | cut -d: -f1 |  xargs -t -n1 tmux kill-session -t

With comments/explanation:

tmux list-sessions   | # list all tmux sessions
  grep -v attached   | # grep for all lines that do NOT contain the pattern "attached"
  cut -d: -f1        | # cut with the separator ":" and select field 1 (the session name)
  xargs -t -n1       ` # -t echoes the command, -n1 limits xargs to 1 argument ` \
  tmux kill-session -t # kill session with target -t passed from xargs

abieler

Posted 2013-08-23T19:31:54.003

Reputation: 21

1Can you put some description of what you're actually doing here?

Also, this will kill all attached sessions, you should note this. – djsmiley2k TMW – 2017-04-05T09:19:23.240

@djsmiley2k All detached sessions you mean (-v flag). – Bart Louwers – 2017-12-09T00:02:10.987