How to Remove Old Docker Containers: A Clean-Up Guide

Over time, Docker containers accumulate on your system — some exited, some unused — taking up valuable disk space. Knowing how to remove old Docker containers is crucial to keeping your system clean, efficient, and manageable.

In this guide, you’ll learn how to list, remove, and automate the cleanup of old or unused Docker containers safely.


🔍 Step 1: View All Containers

Before removing anything, it’s a good idea to see what containers exist.

docker ps -a
  • docker ps: Lists only running containers.
  • docker ps -a: Lists all containers, including exited or stopped ones.

🗑️ Step 2: Remove a Specific Container

If you want to delete a specific container, use:

docker rm <container_id>

Example:

docker rm 87d7e0725b44

You can also use the container name if you prefer.


🚮 Step 3: Remove All Exited Containers

To clean up all containers that have stopped (status = Exited):

docker container prune

You’ll be prompted to confirm.

To skip the confirmation:

docker container prune -f

⚠️ Optional: Remove All Containers (Stopped or Running)

Caution: This will delete every container on your system.

docker rm -f $(docker ps -aq)

Explanation:

  • docker ps -aq: Gets IDs of all containers.
  • docker rm -f: Forcefully removes them, including running containers.

🔁 Automate Cleanup with a Cron Job (Linux)

If you’re managing a development environment and want to automatically remove old containers:

crontab -e

Add this line to remove exited containers daily:

0 3 * * * docker container prune -f

This runs cleanup every day at 3 AM.


🧼 Bonus: Clean Unused Docker Resources

For a full cleanup (containers, networks, volumes, and images not used by any containers):

docker system prune

For a deeper cleanup (including volumes):

docker system prune --volumes

✅ Summary

ActionCommand
List all containersdocker ps -a
Remove a specific containerdocker rm <id>
Remove all exited containersdocker container prune
Remove all containersdocker rm -f $(docker ps -aq)
Full cleanupdocker system prune

🧠 Final Thoughts

Regularly cleaning up old Docker containers ensures:

  • You free up disk space
  • Docker stays fast and efficient
  • Your development environment remains organized

Be sure to double-check what you’re removing, especially on production systems.

Sharing Is Caring:

Leave a Comment