DEV Community

Cover image for How to clean up disk space on Ubuntu server
crazyoptimist
crazyoptimist

Posted on • Updated on

How to clean up disk space on Ubuntu server

If you are running your own cloud instances and use them for development purposes, their capacities would probably be limited and sometimes you might notice that the disk space is too low.

TL;DR

Remove unnecessary system packages and package caches

sudo apt-get --purge autoremove
sudo apt-get autoclean
sudo apt-get clean
Enter fullscreen mode Exit fullscreen mode

Remove system log except for the last 3 days

Check it first:

journalctl --disk-usage
Enter fullscreen mode Exit fullscreen mode
sudo journalctl --vacuum-time=3d
Enter fullscreen mode Exit fullscreen mode

Actually it grows day by day, so you can create a cron script to clean up journal logs once a month for example, or can configure it to use limited amount of disk space by setting SystemMaxUse=50M in /etc/systemd/journald.conf. Then reload conf:

sudo systemctl restart systemd-journald
Enter fullscreen mode Exit fullscreen mode

Clean up snap

Check it first:

du -h /var/lib/snapd/snaps
Enter fullscreen mode Exit fullscreen mode

This is a bash script.

#!/bin/bash
# Removes old revisions of snaps
# CLOSE ALL SNAPS BEFORE RUNNING THIS
set -eu
snap list --all | awk '/disabled/{print $1, $3}' |
    while read snapname revision; do
        snap remove "$snapname" --revision="$revision"
    done
Enter fullscreen mode Exit fullscreen mode

Please add more in the comment section if you have another ones.

Happy coding! :)

Top comments (0)