How to Optimize and Free Up Disk Space on Debian/Ubuntu Servers with Docker Containers
Tags: debian, diskusage, docker, servermaintenance
Published: 2024-06-27 22:36:19

---
title: How to Optimize and Free Up Disk Space on Debian/Ubuntu Servers with Docker Containers
published: true
date: 2024-06-27 07:00:00 UTC
tags: debian,diskusage,docker,servermaintenance
canonical_url: https://diegocarrasco.com/optimize-free-up-disk-space-debian-ubuntu-docker/
---

![](https://diegocarrasco.com/images/social-images/optimize-free-up-disk-space-debian-ubuntu-docker.jpg)

 
## TLDR

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

## Context

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.

## Steps

### 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

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](https://docs.docker.com/engine/reference/commandline/system_prune/))

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](https://docs.docker.com/engine/reference/commandline/container_prune/) 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](https://www.howtogeek.com/devops/what-are-dangling-docker-images/)
- Untagged layers that serve no purpose but still consume disk space.
- Not automatically removed by Docker and need to be cleaned up manually. [source](https://www.baeldung.com/ops/docker-remove-dangling-unused-images)

```
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

```

## References

- [How to Free Up Space on Ubuntu Linux](https://itsfoss.com/free-up-space-ubuntu-linux/)
- [Docker System DF Command Documentation](https://docs.docker.com/engine/reference/commandline/system_df/)

## Related tools I found interesting during my search

- [Docuum Tool for Docker Garbage Collection](https://github.com/stepchowfun/docuum)