As you develop and deploy applications with Docker, your system can quickly accumulate stopped containers that take up space and clutter your environment. These old containers serve no purpose once they’ve exited or crashed, and regularly cleaning them up is a good Docker hygiene practice.
In this blog, you’ll learn how to remove old Docker containers—manually and automatically—along with best practices to keep your system clean.
🔍 What Are “Old” Docker Containers?
Old containers are typically:
- Exited containers (containers that have stopped)
- Unused containers from previous builds, tests, or development cycles
- Dangling containers left behind after image updates or deployments
✅ Step 1: View All Containers
First, list all containers (including stopped ones):
docker ps -a
You’ll see output like:
CONTAINER ID IMAGE STATUS NAMES
123abc my-app Exited (0) 2 days ago my-app-old
456def nginx Up 3 hours web-server
Look for containers with a status of “Exited” or “Created.”
✅ Step 2: Remove a Specific Container
To delete a specific container:
docker rm <container_id_or_name>
Example:
docker rm 123abc
🛑 This only works if the container is not running. You’ll need to stop it first with
docker stop
.
✅ Step 3: Remove All Exited Containers
To remove all containers that have exited:
docker container prune
Output:
Docker will prompt you:
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N]
Type y
to confirm.
🧼 This is the safest and easiest way to clean up old containers.
✅ Step 4: Remove All Containers (Use with Caution)
If you want to remove all containers, regardless of their state:
docker rm $(docker ps -aq)
⚠️ This will delete both running and stopped containers, so be absolutely sure you no longer need any of them.
🧠 Optional: Stop All Running Containers First
If you want to remove all containers but need to stop them first:
docker stop $(docker ps -q)
docker rm $(docker ps -aq)
This will:
- Stop all running containers
- Delete all containers (running and stopped)
✅ Automation: Clean Up Regularly
To prevent clutter, schedule regular cleanups using a cron job or Docker system prune:
docker system prune -f
💡 This will remove stopped containers, unused images, networks, and build cache. Add
--volumes
if you want to remove unused volumes too.
🔒 Best Practices for Removing Containers
- Don’t remove running containers unless you’re sure they’re no longer needed.
- Use
docker container prune
to safely remove exited containers. - Periodically clean up using
docker system prune
to avoid disk space issues. - Tag important containers or manage them with Docker Compose to avoid accidental deletion.
✅ Summary
Task | Command |
---|---|
List all containers | docker ps -a |
Remove a specific container | docker rm <container_id> |
Remove all exited containers | docker container prune |
Remove all containers | docker rm $(docker ps -aq) |
Stop + remove all containers | docker stop $(docker ps -q) + rm |
Full cleanup (containers/images) | docker system prune -f |
Keeping your Docker environment clean improves performance, saves disk space, and helps avoid conflicts. Regularly removing old containers is a simple but powerful practice for any developer or DevOps engineer.