beam-logo
← All posts
Tutorials

How to Use Docker Prune

Eli MernitEli Mernit
January 11, 20253 min read
How to Use Docker Prune

Docker is an important tool for containerizing and deploying applications.

As you build and test your containers, unused images can take up space on your local machine and slow down its performance.

Luckily, this is easily fixable by using the Docker prune command.

In this article, we’ll explain what docker prune is, how to use it effectively, and best practices to keep your local Docker environment running fast and smoothly.

What Is docker prune?

Docker provides the prune command as a simple way to remove unused Docker resources. These resources include:

Containers: Stopped containers that are no longer needed.

Images: Untagged or dangling images that are not associated with any container.

Networks: Unused Docker networks.

Volumes: Unused volumes that are not referenced by any container.

Docker offers granular prune commands, as well as a single command to clean up everything:

docker container prune

docker image prune

docker network prune

docker volume prune

docker system prune

Using docker prune

1. Removing Stopped Containers

As you run and test applications, stopped containers can build up on your machine. To remove all stopped containers:

docker container prune

Docker will prompt for confirmation:

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

If you want to skip the prompt, use the -f (force) flag:

docker container prune -f

2. Cleaning Up Dangling Images

Dangling images are layers of images that are no longer tagged or associated with any container. To remove these, run:

docker image prune

And to remove all unused images (not just dangling ones), use:

docker image prune -a

3. Clearing Unused Networks

Docker networks that are not attached to any containers can also be removed:

docker network prune

4. Deleting Unused Volumes

Unused volumes can take up a lot of disk space over time. To remove them, run this:

docker volume prune

5. Full Cleanup

To clean up all unused containers, networks, images, and optionally volumes, you can run:

docker system prune

For an even deeper cleanup that includes unused volumes:

docker system prune -a --volumes

Warning: Be careful when using -a and --volumes, since they can delete resources that are still being used.

Best Practices for Using docker prune

Review Before Pruning! Before running docker prune, review your Docker environment using commands like docker ps -a, docker images, and docker volume ls to make sure you don’t accidentally delete something important.

Use Filters You can add filters to prune commands to target specific resources. For example, to remove containers that have been stopped for more than 24 hours you can run:

docker container prune --filter "until=24h"

Automate Cleanup If you frequently work with Docker, it's worth automating these cleanup tasks using a cron job or adding them to a CI/CD pipeline.

Monitor Disk Usage Use docker system df to monitor how much disk space Docker is using on an ongoing basis.

Be Mindful of Aggressively Pruning Shared Resources. Avoid aggressive pruning (-a and --volumes) on shared environments, especially where multiple developers or applications rely on Docker.

Conclusion

If you've used Docker, you've probably accumulated a ton of unused resources that slow down your machine. By using the docker prune command, you can save disk space and improve performance.

However, it’s important to use this command with care, especially in production or shared environments.

Whether you’re a seasoned Docker user or just starting out, mastering docker prune will help you keep your system organized and your workflow fast.

Eli Mernit
Eli Mernit
Published January 11, 2025
$30 free creditrefreshed monthly

Start shipping on infra
you won’t outgrow.

Run sandboxes and GPU workloads on your cloud, and scale out to ours when you need to. No infra to manage.