How to delete a detached tmux session?

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?


Solution 1:

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]

Solution 2:

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

tmux list-sessions -F '#{session_attached} #{session_id}' | \
  awk '/^0/{print $2}' | \
  xargs -n 1 tmux kill-session -t

This solution is more robust than the one proposed by abieler because it mathces all detached sessions by ID, not matter what their name is (the solution by abieler would skip a detached session called attached).

Solution 3:

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