Terminus by Warp
Docker Remove Stopped Containers

Docker Remove Stopped Containers

Razvan Ludosanu
Razvan Ludosanu
Founder, learnbackend.dev
Docker

The short answer

To remove (prune) all stopped Docker containers at once, you can use the following command:

 $ docker container prune

If you want to remove these containers without being prompted for confirmation, you can use the [.inline-code]-f[.inline-code] flag (short for force):

 $ docker container prune -f

[#warp-workflows]Use Warp's Workflows feature to easily recall this syntax[#warp-workflows]

If you’re using Warp as your terminal and you need to quickly retrieve this command, you can use Warp's Workflows feature by pressing [.inline-code]CTRL-SHIFT-R[.inline-code] and typing [.inline-code]remove stopped containers[.inline-code]:

Then pressing [.inline-code]ENTER[.inline-code] to use the suggested command:

[#filtering-by-creation-time]Filtering stopped containers by creation time[#filtering-by-creation-time]

The [.inline-code]docker container prune[.inline-code] command offers the possibility to filter the containers that will be removed using the [.inline-code]--filter[.inline-code] flag.

To remove all stopped containers that were created before a certain time, you can use the [.inline-code]until=<timestamp>[.inline-code] filter, where [.inline-code]timestamp[.inline-code] can be a Unix timestamp, a date formatted timestamp, or Go duration string.

For example:

 $ docker container prune --filter "until=10m"
 $ docker container prune --filter "until=2023-01-15"

[#removing-all-stopped-containers]Removing all stopped containers using [.inline-code]docker rm[.inline-code][#removing-all-stopped-containers]

The [.inline-code]docker rm[.inline-code] command is used to remove one or more stopped containers using their name or ID.

 $ docker rm  …
 $ docker rm 

[#list-show-IDs]List and show the IDs of stopped containers with [.inline-code]docker ps[.inline-code][#list-show-IDs]

To get a list of all stopped containers IDs – which are containers with a status equivalent to [.inline-code]exited[.inline-code] or [.inline-code]dead[.inline-code] – you can use the [.inline-code]docker ps[.inline-code] command combined with the [.inline-code]--filter[.inline-code] and the [.inline-code]-q[.inline-code] flags:

 $ docker ps --filter "status=exited" --filter "status=dead" -q

Where:

  • The [.inline-code]--filter[.inline-code] flag is used to filter the list of containers returned by the [.inline-code]docker ps[.inline-code] command.
  • The [.inline-code]-q[.inline-code] flag is used to return their IDs only.

[#removing-stopped-containers]Removing all stopped containers[#removing-stopped-containers]

To remove all stopped containers at once with [.inline-code]docker rm[.inline-code], you can combine it with the previous [.inline-code]docker ps[.inline-code] command using the command substitution syntax:

 $ docker rm $(docker ps --filter "status=exited" --filter "status=dead" -q)

Where the expression contained in parenthesis [.inline-code]$(expression)[.inline-code] will be replaced by its result, which in this case will be the list of all stopped containers IDs.

Experience the power of Warp

  • Write with an IDE-style editor
  • Easily navigate through output
  • Save commands to reuse later
  • Ask Warp AI to explain or debug
  • Customize keybindings and launch configs
  • Pick from preloaded themes or design your own