Root drive is running out of disk space. How can I free up space?

I'm using Ubuntu 11.04 and I want to free up some space in my root directory, which is overloaded. I especially want to change the path used for installing applications (they are getting installed directly to the root drive).

Another consideration is that I'm working on a MySQL database server. The server is installed in the root directory itself, so I don't want to risk losing any data.

Please give me some tips to help sort out this problem.


I successfully cleared 3.5 GB by removing old headers and images.

Please note on some systems this also attempts to remove the current kernel which is not a great idea - see comments below for how to check.

I used the following command:

dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d' | xargs sudo apt-get -y purge

You can check what packages will be purged executing the first part of the command:

dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d'

Reference


Recently I faced similar situation. Too many applications got installed and they started using my root mount space. I am listing out few steps which I followed and hoping that you could also use the same.

  1. Clean apt-get cache. Following command will remove all downloaded deb files from apt-get cache directory.

    Run this command: sudo apt-get clean

  2. Move /home mount point to different drive. Previously, my home folder was situated on root drive. So I moved my home folder to separate drive. This helped me to release lot of stress from root mount because most of applications store their data in /home/user_name/ folder. Read how to move home folder to separate drive.

  3. Increase size of root partition I know it is very obvious answer. But believe me, our data need changes over the time. I thought 20 GB /root mount would suffice but withing a year I have re-sized my root mount and increased to 50 GB.


Use dpkg-query to find the largest packages and remove the ones you don't need anymore (source):

dpkg-query --show --showformat='${Package;-50}\t${Installed-Size}\n' | sort -k 2 -n | grep -v deinstall | awk '{printf "%.3f MB \t %s\n", $2/(1024), $1}'

Here is a script I run to free space on root partition

function myclean {
    ## Show free space
    df -Th | grep -v fs
    # Will need English output for processing
    LANG=en_GB.UTF-8

    ## Clean apt cache
    apt-get update
    apt-get -f install
    apt-get -y autoremove
    apt-get clean

    ## Remove old versions of snap packages
    snap list --all | while read snapname ver rev trk pub notes; do
        if [[ $notes = *disabled* ]]; then
            snap remove "$snapname" --revision="$rev"
        fi
    done
    ## Set snap versions retain settings
    if [[ $(snap get system refresh.retain) -ne 2 ]]; then snap set system refresh.retain=2; fi
    rm -f /var/lib/snapd/cache/*

    ## Remove old versions of Linux Kernel
    # This one-liner is deprecated since 18.04
    # dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d' | xargs apt-get -y purge
    # New 2 lines to remove old kernels
    dpkg --list | grep 'linux-image' | awk '{ print $2 }' | sort -V | sed -n '/'"$(uname -r | sed "s/\([0-9.-]*\)-\([^0-9]\+\)/\1/")"'/q;p' | xargs apt-get -y purge
    dpkg --list | grep 'linux-headers' | awk '{ print $2 }' | sort -V | sed -n '/'"$(uname -r | sed "s/\([0-9.-]*\)-\([^0-9]\+\)/\1/")"'/q;p' | xargs apt-get -y purge

    ## Rotate and delete old logs
    /etc/cron.daily/logrotate
    find /var/log -type f -iname *.gz -delete
    journalctl --rotate
    journalctl --vacuum-time=1s

    ## Show free space
    df -Th | grep -v fs
}