If you’ve been using Docker for awhile, you’ll likely notice that your free disk space keeps shrinking, specifically after pulling and updating images. This is because by default, Docker stores images and volumes even if they’re not in use anymore. If you’ve updated your containers, you may also have old versions of images stored that you no longer need.
To free up some storage, we should delete these unnecessary images and volumes. We can use the docker system prune
command to do so.
Free Disk Space by Pruning Unused Images
To remove unused images, use the following command:
docker system prune -a -f
The -a
flag removes all unused images, and the -f
flag skips confirmation.
Free Disk Space by Pruning Unused Volumes
Warning! This command may delete data created by the containers if the container volumes you want to keep aren’t in use. Therefore, use it at your own risk.
To remove unused volumes, use the following command:
docker system prune --volumes -f
The --volumes
flag removes all unused volumes, and the -f
flag skips confirmation.
Pruning Both Unused Images & Volumes
Warning! This command may delete data created by the containers if the container volumes you want to keep aren’t in use. Use it at your own risk.
To remove unused images and volumes together, use the following command:
docker system prune -a --volumes -f
The -a
flag removes all unused images, the --volumes
flag removes all unused volumes, and the -f
flag skips confirmation.
Scheduling Pruning of Unused Images & Volumes
Unfortunately, at this time there is no way that is built into Docker to periodically prune unused images and volumes automatically.
However, if you’re on a *nix system, BSD system or macOS, you can use cron to periodically run one of the commands above periodically.
If you’re on Windows, you can use Task Scheduler to trigger one of the commands above.
Conclusion
By pruning unused images and volumes using the docker system prune
command, you can often save tens of gigabytes of storage!
To see all the available flags for this command, please refer to the Docker documentation.
Leave a Reply