Remove all stopped containers in Docker using prune command


As you keep creating new docker containers from images (without --rm option) you will see that you start accumulating stopped containers that take up space, you can check them by running the docker ps command with -a option.

Example:
% docker ps -a

CONTAINER ID   IMAGE     COMMAND         CREATED          STATUS                      PORTS     NAMES
e9072f3eff69   fedora    "echo hello"   17 minutes ago   Exited (0) 17 minutes ago             thirsty_almeida
21abcc335be3   ubuntu    "echo 12345"   17 minutes ago   Exited (0) 17 minutes ago             interesting_turing
0c0ba20b0018   fedora    "echo hello"   18 minutes ago   Exited (0) 18 minutes ago             cool_robinson

If you do not need them and so to remove all the stopped containers we can make use of the docker container prune command,

Example:
% docker container prune

WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y

Deleted Containers:
e9072f3eff695c461c505a00a8ec63037994daad1df6fd5e41ffa9c10fc26c42
21abcc335be31c6f95312a88bf22738cfe79fb735f864a5b3532f5cf439dbbe8
0c0ba20b00189a16c383d32fc61b9189beddbb6d94454ef9f5617315a95c9229

Total reclaimed space: 124B

Remove Containers based on time lasped since created using until filter

Prune all docker containers that were created 10 minutes from now using --filter option,

% docker container prune --filter "until=10m"
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y

Deleted Containers:
e9072f3eff695c461c505a00a8ec63037994daad1df6fd5e41ffa9c10fc26c42

Total reclaimed space: 0B
Docker Delete All Stopped Containers using Prune Command

If you do not want to see the prompt as you run the prune command you can make use of the -f or --force option.



Delete a specific Stopped Docker Container

If you want to remove a specific docker container you can make use of the docker rm command followed by the container name,

% docker rm charming_antonelli
charming_antonelli


Have Questions? Post them here!
Advertisements
Try Out Code2care Dev Tools:

Advertisements

Advertisements
Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and software developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include learnings, tutorials, trouble-shooting steps, video tutorials, code snippets, how-to, blogs, articles, etc.

🎉 We are celebrating the 10th years of Code2care! Thank you for all your support!

We strongly support Gender Equality & Diversity.