246

My Ubuntu cloud server has left only 900MB of disk space.

I'll just empty the directory /tmp and wondering if there is any other location to clean up.

Mateo
  • 8,104
user3215
  • 5,383

18 Answers18

267

To delete downloaded packages (.deb) already installed (and no longer needed)

sudo apt-get clean

To remove all stored archives in your cache for packages that can not be downloaded anymore (thus packages that are no longer in the repository or that have a newer version in the repository).

sudo apt-get autoclean

To remove unnecessary packages (After uninstalling an app there could be packages you don't need anymore)

sudo apt-get autoremove

To delete old kernel versions

sudo apt-get remove --purge linux-image-X.X.XX-XX-generic

If you don't know which kernel version to remove

dpkg --get-selections | grep linux-image

Source: Limpiando Ubuntu: comandos y programas (actualización) (google translated)

rubo77
  • 32,486
DrKenobi
  • 6,442
234
  • Show top 10 biggest subdirs in the current dir.

     du -sk * | sort -nr | head -10
    

you can create this as an alias in your say: ~/.bashrc and use it

    alias ducks='du -cks * | sort -rn | head'
  • Use File Usage Analyzer (AKA baobab; GNOME based), Filelight or kDirStat (KDE based), to see where the disk space is going visually (ncdu uses a TUI).

  • Check if you have old kernels for deletion

     ls -lh /boot
    
  • Cleaning packages

     sudo apt autoremove
     sudo apt autoclean
    

    see list of all installed packages, sorted by size. If you see something big and don't use it - uninstall it

     dpkg-query -W --showformat='${Installed-Size} ${Package}\n' | sort -nr | less
    
  • Clean unused language files with translations (there are tons of them)

     sudo apt install localepurge
    
  • Check content of /var/tmp/

     du -sh /var/tmp/
    
  • Check also

     man deborphan
    
  • Search for big files:

     find / -type f -size +1024k
    

    or

     find / -size +50000  -exec ls -lahg {} \;
    
  • Big installed packages (part of the package: debian-goodies)

     dpigs
    

    or wajig sizes | tail -30.

  • On systemd: Remove the oldest archived journal files until the disk space they use falls below the specified size

     sudo journalctl --vacuum-size 10M
    
  • Limit Tracker disk usage.

jet
  • 7,274
  • 11
    To find big files I suggest ncdu, which provides a fast way to see what directories are using your disk space – rubo77 Jan 22 '15 at 05:18
  • 4
    kDirStat is replaced by baobab – rubo77 Jan 22 '15 at 06:23
  • 1
    After running sudo journalctl --vacuum-size 10M I am getting Warning: journal has been rotated since unit was started, output may be incomplete. message , how could I fix it? – alper Jan 01 '22 at 15:30
  • baobab is wow Bob! – Fuhrmanator Jan 01 '22 at 16:50
  • @rubo77 ncdu is not fast compared to baobab, at least on my WSL 20.04 - because it gets confused by windows/linux file systems /mnt/c/... and scans my entire drive, not just WSL. – Fuhrmanator Jan 01 '22 at 17:06
  • 1
    ncdu / -x is a good use in WSL (to avoid scanning outside disks) – Fuhrmanator Jan 01 '22 at 17:12
  • Your dpkg-query line gives me many linux-modules-extra-5.x.x.x-generic. and also a bunch of somewhat smaller linux-image-4.x.x.x-generic. How can I uninstall all of these?? How can I uninstall only the ones I don't need? – CPBL Mar 17 '22 at 23:55
  • @jet (Or, actually: is there a problem in your command? It's displaying the installed size, even for packages that are not installed ! ) – CPBL Mar 18 '22 at 00:09
128
sudo apt-get autoremove

That can clean out a lot of guff (old kernels, etc) that have been replaced. You can do a similar thing in Synaptic (load it up and select the status button and then the Auto-removeable option).

Oli
  • 293,335
  • will it not affect running applications like if it removes any necessary package on which any application depends?. – user3215 Oct 11 '10 at 13:54
  • 1
    No. Autoremove looks for orphaned packages, that is to say packages that you didn't explicitly install yourself (ones marked auto) and that have no dependencies. If you use aptitude instead of apt-get, it can clean up as you go but it's quite common for older installations to collect a raft of installed kernels as security updates come out. – Oli Oct 11 '10 at 14:03
  • Thank you!. I have atleast 1.1GB of free space. How about using apt-get autoremove? – user3215 Oct 11 '10 at 14:10
  • 1
    Typo in my last comment. I meant to say that orphaned packages are ones that aren't depended on, not ones that "have no dependencies". They may well have dependencies and if they were only there for that orphaned package, they'll be removed too. Some times (with really deep dependency trees) it can take a couple of autoremoves to clean up. – Oli Oct 11 '10 at 14:28
  • 5
    While the above command is certainly useful, it will not, as stated, remove old kernels. That has to be done either manually or with additional tools like Ubuntu Tweak. – codeling Aug 17 '13 at 08:33
  • 1
    @nyarlathotep It will remove kernel header files, which at first glance may make one think it's removing the old kernels - the old kernels remain, however (try dpkg --get-selections | grep linux-image). – drevicko Dec 01 '13 at 23:55
27

I just freed up almost 2 gigs by removing old kernels and header files:

use

uname -r

to check your current version, then

dpkg -l linux-image-* linux-headers-*

to see all the old kernels and header files, then

sudo apt-get remove linux-image-<XYZ> linux-headers-<XYZ>

the apt-get remove command supports wildcards, so you can do apt-get remove linux-image 3.0.* linux-headers-3.0.* for example, to get rid of many at once.

Make sure you don't kill remove current kernel of course! And maybe keep one or two old version, just in case... but not 10 or 20!

hwjp
  • 1,387
  • That bit about wildcards ? Before making use of it I would double-check that because I tried it and I got more than I wanted. It's possible (perhaps even likely) that I stuffed up in some way but I don't know how and yet I lost more headers than I wanted . – glaucon Oct 25 '16 at 21:28
  • The wildcards are regex wildcards rather than globs. – Chai T. Rex Sep 24 '18 at 15:04
  • There should be some autoremove for old kernel... – Nam G VU Jan 02 '23 at 02:45
22

Do not forget to Empty Trash.

screenshot

Or from command line:

rm -r ~/.local/share/Trash/info/ && rm -r ~/.local/share/Trash/files/  

Or just trash-empty using trash-cli package.

Pablo Bianchi
  • 15,657
desgua
  • 32,917
  • rm.sh: ~/.local/share/Trash/info/: No such file or directory can I directly do command rm -rf ~/.local/share/Trash/*? – alper Mar 26 '21 at 21:02
  • Why you are trying "rm.sh" instead of "rm"? – desgua Mar 28 '21 at 10:31
  • I was using shell-safe-rm (https://github.com/kaelzhang/shell-safe-rm) – alper Mar 28 '21 at 18:52
  • Notice that with "shell-safe" you won't actually delete any file, as they state at github site "Using safe-rm, the files or directories you choose to remove will move to $HOME/.Trash instead of simply deleting them". – desgua Mar 31 '21 at 16:23
19

Uninstall large packages that you don't use

Some packages can be quite large. A convenient way of finding these is to use dpigs. It's not installed by default but can be found in debian-goodies (GitHub project)

sudo apt-get install debian-goodies

Then:

$ dpigs
419576 texlive-latex-extra-doc
204112 nvidia-319
175463 google-chrome-stable
141058 linux-image-extra-3.11.0-15-generic
113173 libreoffice-core
104822 valgrind
102322 qt4-doc
93337 blender
91105 texlive-pstricks-doc
90517 libboost1.53-dev

There are a few options:

-n, --lines=N
  Display the N largest packages on the system (default 10).
-s, --status=status-file
  Use status-file instead of the default dpkg status file.
-S, --source
  Display the largest source packages of binary packages installed
  on the system.
-H, --human-readable
  Display package sizes in human-readable format (like ls -lh or du -h)
-h, --help
  Display this message.
Drew Noakes
  • 5,708
17

I really recommend you the program BleachBit, which cleans everything on Ubuntu. Install it using the following command:

sudo apt install bleachbit
Pablo Bianchi
  • 15,657
  • Would be nice as you can share more details at which cleans everything on Ubuntu > A great graphical GUI tool to fit all needs https://askubuntu.com/a/18459/349837 – Nam G VU Jan 02 '23 at 02:51
17

Remove all old kernel versions automatically

Attention: If you've just upgraded the kernel, reboot before deleting the older versions!

Remember to check which kernel you are using type:

uname -r

Then as root:

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

read that thread to be safe not to remove needed kernels with this command!

If you run into problems, use this script instead: https://askubuntu.com/a/1409370/34298

rubo77
  • 32,486
14

Check for large numbers of log files too:

sudo du -h /var/log

Or as rubo77 points out in commends, you could use the NCurses disk usage tool:

sudo ncdu /var/log
Drew Noakes
  • 5,708
14

Cleaner script

This script will execute the biggest chunks on the console:

  • Clean apt cache
  • Remove config files left from uninstalled .deb packages (happens if you don't use the --purge switch with apt-get)
  • Remove every kernel except the one you are using
  • Empty the trashes of every user(including root)

create a file with this content and give it executable rights:

#!/bin/bash
# Adapted from 71529-ubucleaner.sh - https://web.archive.org/web/20151209182520/http://opendesktop.org/CONTENT/content-files/71529-ubucleaner.sh

OLDCONF=$(dpkg -l|grep "^rc"|awk '{print $2}') CURKERNEL=$(uname -r|sed 's/-*[a-z]//g'|sed 's/-386//g') LINUXPKG="linux-(image|headers|ubuntu-modules|restricted-modules)" METALINUXPKG="linux-(image|headers|restricted-modules)-(generic|i386|server|common|rt|xen)" OLDKERNELS=$(dpkg -l|awk '{print $2}'|grep -E $LINUXPKG |grep -vE $METALINUXPKG|grep -v $CURKERNEL) YELLOW="\033[1;33m"; RED="\033[0;31m"; ENDCOLOR="\033[0m"

if [ $USER != root ]; then echo -e $RED"Error: must be root! Exiting..."$ENDCOLOR exit 0 fi

echo -e $YELLOW"Cleaning apt ..."$ENDCOLOR aptitude clean apt-get autoremove apt-get autoclean

echo -e $YELLOW"Those packages were uninstalled without --purge:"$ENDCOLOR echo $OLDCONF #apt-get purge "$OLDCONF" # fixes the error in the original script for PKGNAME in $OLDCONF ; do # a better way to handle errors echo -e $YELLOW"Purge package $PKGNAME" apt-cache show "$PKGNAME"|grep Description: -A3 apt-get -y purge "$PKGNAME" done

echo -e $YELLOW"Removing old kernels..."$ENDCOLOR echo current kernel you are using: uname -a aptitude purge $OLDKERNELS

echo -e $YELLOW"Emptying every trashes..."$ENDCOLOR rm -rf /home//.local/share/Trash//** &> /dev/null rm -rf /root/.local/share/Trash//* &> /dev/null

echo -e $YELLOW"Script Finished!"$ENDCOLOR

Adapted from 71529-ubucleaner.sh

Find large folders and packages

Some tools that will help you find large folders and packages:

sudo apt install ncdu debian-goodies deborphan
sudo ncdu -xr /  # lists all folders by size on the console (like the gui `baobab`)
dpigs -H         # shows large packages that you don't use
man deborphan    # finds packages that have no packages depending on them
deborphan --guess-all --libdevel | xargs apt-get -s purge

Localepurge

Use localepurge to uninstall unused languages in your system:

sudo apt install localepurge
sudo localepurge

Remove old snapd images

LANG=c snap list --all | awk '/disabled/{print $1, $3}' | \
    while read snapname revision; do \
        snap remove "$snapname" --revision="$revision"; \
    done

More here.

Bleachbit

A great graphical GUI tool to fit all needs:

sudo apt install bleachbit

More info about Bleachbit here.

Clear pythons pip cache

pip cache purge
sudo pip cache purge

Check your temp and cache folder

ncdu /var/tmp
ncdu /var/cache

Remove manpages and documentation

Additionally you can remove manpages and documentation as described in the Ubuntu Wiki:

Create a file /etc/dpkg/dpkg.cfg.d/01_nodoc which specifies the desired filters. Example:

path-exclude /usr/share/doc/*
# if we need to keep copyright files for legal reasons:
# path-include /usr/share/doc/*/copyright
path-exclude /usr/share/man/*
path-exclude /usr/share/groff/*
path-exclude /usr/share/info/*
# lintian stuff is small, but really unnecessary
path-exclude /usr/share/lintian/*
path-exclude /usr/share/linda/*

Remove the same set of files and directories in the project-config's postinst. Example:

echo "Removing documentation..."
# if we need to keep copyright files for legal reasons:
# find /usr/share/doc -depth -type f ! -name copyright | xargs rm || true
# else:
find /usr/share/doc -depth -type f | xargs rm || true
rm -rf /usr/share/man/* /usr/share/groff/* /usr/share/info/* /usr/share/lintian/* /usr/share/linda/* /var/cache/man/*
rubo77
  • 32,486
  • I got a warning executing this script that I was about to remove a kernel with the same version number as I was running. – Christophe De Troyer Mar 13 '17 at 13:01
  • check the current kernel you are using with uname -a. In case you just did an apt-get ugrade before and got a new kernel, this could be the cause, that the current kurnel you are using is not the newest that is installed? Do a repoot then before starting this script – rubo77 Mar 14 '17 at 17:25
  • I find debfoster better than deborphan. Also, popcon-largest-unused. Someone already mentioned kdirstat and bleachbit (which IIRC has a dupes finder). – pbhj Jan 03 '23 at 18:44
  • Depending on your case, you can find some more options in this script: https://github.com/boxcutter/debian/blob/main/script/minimize.sh – rubo77 Jan 04 '23 at 22:39
10

Try using BleachBit (located at sourceforge). It is a great program. the basic idea is that it quickly frees up disk space and removes a lot of the junk that is hidden in the system. There are about 70 applications that it can recognize and wipe clean. There is also the ability to use it to "wipe" the free disk space. I think of it as CCleaner from windows only for linux.

RunningUtes
  • 3,932
8

If you use UFW, check the log folder. On one of my machines, UFW was generating 8Gb of logs in a couple of days.

You could also empty the apt cache with:

sudo apt-get clean
lovinglinux
  • 6,367
5

First, there is a tool for listing all big folders and files. Just type 'Baobab' into the launcher. By removing folders and files you don't need you get more space.


Then, there is a program for removing double files. The program is called fslint. Install it by entering

sudo apt-get install fslint

in a Terminal.

or graphically

Install fslint via the software center


You can also clear the package cache with

sudo apt-get autoclean
sudo apt-get clean
sudo apt-get autoremove

The fourth step is to remove old Kernel entries. You can do this by installing and opening Synaptic. In Synaptic, search for the old Kernel entries (every Kernel entry that is displayed in GRUB except the newest) and remove it.


One good step is to remove the application cache. Do this by installing and running bleachbit:

sudo apt-get install bleachbit

or graphically

Install bleachbit via the software center

Important: Deleted cache cannot be restored!


The last step is to defragment the file system. Do the following for this:

cd $HOME
git clone git://git.kernel.org/pub/scm/fs/ext2/e2fsprogs.git
cd e2fsprogs
./configure
make

With these commands, you download and compile e2fsprogs. (If you or the community has got a better way to install the program, please edit!)

Also make sure that git-core is installed. If not, execute:

sudo apt-get install git-core 

or using Software Center Install git-core via the software center

Now you can run the program with:

cd $HOME/e2fsprogs/misc
./e4defrag /dev/sda1    #defragment /dev/sda1
./e4defrag -v /dev/sda1 # verbose output
./e4defrag -c /dev/sda1 # see overview of fragmentation status

Replace /dev/sda1 with the file system or folder/file you want. Note that for defragmenting a device (such as HDD) you need root privilegs, but not for your own files.


Source (German): here

slashcrack
  • 1,083
  • Wow, deleting old linux-header and linux-image versions freed up about a gig and a half on my 10.04 install. fslint is a great find as well. Thanks. – braddock Jul 15 '12 at 23:13
  • Instead of baobab you can also use the commandline tool ncdu to find large folders and files: sudo apt-get install ncdu – rubo77 Jan 22 '15 at 05:19
3

The /tmp directory should never be cleaned up manually unless absolutely necessary (ie: a closed application didn't clean-up after itself).

You can search where space might be filling up using the 'du' command; usually places of interest are inside /var. To name two usual suspects /var/log and '/var/cache'. Though I've had some users who install a backup utility and never realise that it creates GBs of incremental backups in /var/backup that build up over a week or month to fill the entire disk space.

alper
  • 222
ibuclaw
  • 420
  • Thank you!. As you said I have some space occupied in MBs under /var/cache and wondering how to remove unnecessary files. – user3215 Oct 11 '10 at 14:13
2

If you have already deleted a lot of junk but disk space does not seem cleaned just reboot ubuntu. Or if you don't want to reboot just run command like in similar question.

sudo service rsyslog restart
2

Use ubuntu-tweak to clean.

it cleans all *.deb that are downloaded to installation apps.

rubo77
  • 32,486
mwm
  • 435
1

You can try clearing the cache and error reports i had about 5 gigs of such junk when i tried this first. I used stacer. You can do it manually also.

  • install stacer sudo apt install stacer
  • stacer is a gui application so you will know what to do. if you don't, launch stacer then click system cleaner (has broom icon on it) click select all then click the magnifying glass select stuff you want to delete then click broom icon again.
Pablo Bianchi
  • 15,657
0

When I need make more free space on servers I use this command. It find all files bigger then 50 MB and "du -h" make better list of files and "sort -n" after pipe make list numericcaly sorted by file size.

find / -size +50M -type f -exec du -h {} \; | sort -n
zorbon.cz
  • 1,177