How to Optimize and Free Up Disk Space on Debian/Ubuntu Servers with Docker Containers


Manage disk space on Debian/Ubuntu servers and Docker containers by removing unnecessary packages, cleaning up caches, and pruning Docker objects.


I needed to free up space as I had a small VPS with full storage, and my notebook and desktop computers had also a really high disk usage, although I did not have that many files, but I do use a lot of docker.

After researching I did not find a guide with everything I needed (explanations included), thus here it is.


Package Manager (apt)

Remove packages that are no longer required

sudo apt-get autoremove

Clean Up APT Cache

Check the space used by the APT cache:

sudo du -sh /var/cache/apt

Clean up the APT cache:

sudo apt-get autoclean
sudo apt autoclean

Delete cache files:

sudo apt-get clean
sudo apt clean

Clear Systemd Journal Logs

Check the disk usage of systemd journal logs:

journalctl --disk-usage

Clean logs older than 3 days:

sudo journalctl --vacuum-time=3d


Docker takes a lot of space compared to vanilla servers. Check link:/slug/change-docker-data-directory-vps-optimization for a related post on the overlay2 and how to move docker data root to another volume/ drive.

Check system usage

Check overall system usage:

docker system df

For more detailed information:

docker system df -v

Use docker system prune

(from documentation)

WARNING! This will remove:

  • all stopped containers
  • all networks not used by at least one container
  • all dangling images
  • all build cache
docker system prune
Use Docker Container Prune

Warning: This will remove all stopped containers. Refer to the documentation for more details.

docker container prune # Remove all stopped containers
Use Docker Image Prune

Remove unused images (Remove all dangling images. If -a is specified, will also remove all images not referenced by any container.)

Quick note:

What are Docker Dangling Images?

  • Images that have no tag and are not referenced by any container source
  • Untagged layers that serve no purpose but still consume disk space.
  • Not automatically removed by Docker and need to be cleaned up manually. source
docker image prune

Use docker volume prune

Remove all unused local volumes. Unused local volumes are those which are not referenced by any containers. By default, it only removes anonymous volumes.

docker volume prune # remove only anonymous (unnamed) volumes

This command removes only anonymous (unnamed) volumes by default.

To remove all unused volumes:

docker volume prune -a # remove all unused volumes



Comments powered by Disqus