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

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap