Post Date: February 6, 2021
Last Updated: November 26, 2026
Introduction
This guide explains how to remove stopped and unused container objects in Docker with the command line. Every time a container exits, Docker keeps it on disk so you can inspect its logs and restart it. On a busy server, hundreds of exited containers pile up and use disk space, so cleaning them up regularly keeps the host healthy.
The original guide gave the right idea but had commands that fail. The stop-all and remove-all examples passed docker container ls -a into another command, which feeds table text and headings to Docker instead of container IDs. This version fixes them with the -q flag. It also replaces docker system prune with the narrower docker container prune, because system prune removes much more than containers. A verification step and troubleshooting are new.
Warning: Before you remove stopped and unused container objects, check what is stopped. A removed container loses its logs and any data stored inside it. Data in named volumes stays, but anonymous volumes may be deleted with
docker rm -v. Back up anything you need first, and never run cleanup commands on a production host you have not inspected.
Prerequisites
Before you remove stopped and unused container objects, make sure you have:
- Docker Engine installed and running (check with
docker version) - A user in the
dockergroup, or sudo access - SSH or terminal access to the Docker host
- A list of containers you must keep, such as databases and long-running services
- A backup of any data that lives inside a container instead of a volume
Architecture
- The Docker CLI is the
dockercommand you type. It sends each request to the daemon - The Docker daemon (
dockerd) creates, runs, stops and removes containers - A container is running while its main process is alive, and becomes exited (stopped) when that process ends
- A stopped container keeps its writable layer and logs on disk until you remove it
- Images, volumes and networks are separate objects. Removing a container does not remove its image or its named volumes
The workflow is to list, stop what you no longer need, remove the stopped containers, then verify the result. The image below shows the container lifecycle. For the official references, see the Docker documentation for docker container prune and docker system prune.

Steps to Remove Stopped and Unused Container Objects in Docker
1. List All Docker Containers
docker ps -a
The newer form does the same:
docker container ls -a
The STATUS column shows Up for running containers and Exited for stopped ones. A container with the status Created was never started. To list only the stopped ones, add a filter:
docker ps -a --filter "status=exited"
To see how much disk space containers, images and volumes use, run:
docker system df
2. Stop a Running Container
Stop one container by its ID or name. The original guide said the ID is numeric, but it is a hexadecimal string, and you can use the name instead:
docker container stop my-container docker container stop 3f2a9c1b7d4e
To stop every running container, use the -q flag so that only IDs are passed:
docker container stop $(docker container ls -q)
Note: The original used
docker container ls -ahere, which prints table headings and fails. Use-qfor IDs only. Without-a, it lists only running containers, which is what you want when you stop containers.
3. Remove a Single Stopped Container
docker container rm my-container
To also delete the anonymous volumes that belong to that container, add -v:
docker container rm -v my-container
Docker refuses to remove a running container, which protects you from mistakes. Stop it first, or add -f only if you are sure.
4. Remove Stopped and Unused Container Objects in One Command
This is the main step to remove stopped and unused container objects. The safest command is docker container prune, which deletes every stopped container and asks for confirmation:
docker container prune
To skip the confirmation prompt in a script, add -f:
docker container prune -f
To remove only containers that stopped more than 24 hours ago, add a filter:
docker container prune --filter "until=24h"
An alternative is to remove containers by status:
docker container rm $(docker container ls -aq --filter "status=exited")
The original used docker container rm $(docker container ls -a) here, which does not work. The -q flag and the status=exited filter fix it.
5. Understand What docker system prune Removes
The original guide offered docker system prune as an alternative, but it does much more than clean up containers. By default it removes:
- all stopped containers
- all networks not used by a container
- all dangling images (images with no tag)
- the build cache
Adding -a also removes every image not used by a container, and adding --volumes also removes unused volumes, which can delete data. Use it only when you want a full cleanup:
docker system prune
Warning:
docker system prune -a --volumescan delete database data stored in unused volumes. Rundocker volume lsanddocker image lsfirst, and preferdocker container prunewhen you only want to remove stopped and unused container objects.
6. Verify the Cleanup
docker ps -a docker system df
Only the containers you want to keep should remain, and docker ps -a --filter "status=exited" should return no rows. The RECLAIMABLE column in docker system df shows how much space is still unused by images and volumes.
To avoid the buildup in the future, start short-lived containers with --rm, so Docker deletes them as soon as they exit:
docker run --rm alpine echo "hello"
Troubleshooting
If you cannot remove stopped and unused container objects, match the message you see:
- “Error response from daemon: You cannot remove a running container … Stop the container before attempting removal or force remove”: The container is still running. Stop it first with
docker container stop, then remove it - “permission denied while trying to connect to the Docker daemon socket”: Your user cannot reach Docker. Run the command with
sudo, or add your user to thedockergroup and log in again - “Error response from daemon: No such container”: The ID or name has a typo, or the container was already removed. Run
docker ps -aagain - “docker container stop” requires at least 1 argument: The
$(docker container ls -q)list is empty, because no container is running. This is harmless - “conflict: unable to delete … image is being used by stopped container”: You tried to remove an image while a stopped container still uses it. Remove the container first, then the image
- Containers come back after you remove them: A restart policy, Docker Compose or an orchestrator is recreating them. Stop the Compose project with
docker compose down, or change the policy withdocker update --restart=no - The disk is still full after you remove stopped and unused container objects: The space is probably in images, volumes or the build cache. Run
docker system df, then prune images withdocker image prune - “Error response from daemon: removal of container … is already in progress”: Docker is already deleting it. Wait a moment and run
docker ps -aagain
Conclusion
To remove stopped and unused container objects in Docker, list them with docker ps -a, stop what you no longer need, and run docker container prune. Use docker container rm for a single container, and use -q to pass only IDs into other commands. Choose docker container prune over docker system prune unless you want to remove images, networks and the build cache as well. Verify with docker system df, and use --rm for short-lived containers.
Frequently Asked Questions
What is the safest way to remove stopped and unused container objects?
Run docker container prune. It removes only stopped containers, asks for confirmation, and leaves images, networks and volumes alone.
What is the difference between docker container prune and docker system prune?container prune removes stopped containers only. system prune also removes unused networks, dangling images and the build cache, and with extra flags it removes all unused images and volumes.
Does removing a container delete its data?
It deletes the container’s writable layer and logs. Data in named volumes stays. Anonymous volumes are removed only if you use docker rm -v.
How do I remove all stopped containers except one?
Use a name filter in a list, for example docker container ls -aq --filter "status=exited", and remove the IDs you want. Or stop the one you want to keep from being exited by restarting it first.
Can I remove a container that is running?
Not without -f. Stop it first with docker container stop, which lets the process shut down cleanly.
Related Articles
How to Configure Docker Networking Between Containers
How to Configure Docker Container Restart Policies
Talk to DevOps and Cloud Experts
Unused containers and images quietly consume disk space and can slow down servers or cause deployment failures. At Pheonix Solutions, our team helps businesses manage container environments, automate cleanup tasks, configure servers, and maintain reliable application infrastructure.
We can help you with:
- DevOps and cloud services for Docker and Kubernetes setup, container management, CI/CD automation, and infrastructure optimization.
- Software maintenance and support to monitor disk usage, troubleshoot container issues, and keep your servers running smoothly.
- Software development services to build and containerize scalable applications ready for production deployment.
Need help managing Docker containers, cleaning up unused resources, or maintaining your container infrastructure? Talk to our DevOps and Cloud experts to discuss your requirements.
