Knowledge Base:Freeing disk space

Synopsis
This article introduces tools that help to remove unnecessary system files and optimize the filesystem in order to free disk space.

Environment
Although disk space is relatively cheap as of writing this article, it may not be so easy or even possible to expand storage on mobile, embedded or other devices, so freeing useless disk space is often important.

System maintenance related files
Over time large (or a large number of) yet unnecessary files accumulate in certain directories as the system is upgraded:


 * the module files installed after kernel compilation are not tracked by the package manager an thus are not deleted after unmerging
 * the module files installed after kernel compilation are not tracked by the package manager an thus are not deleted after unmerging


 * as with module files, kernel object files aren't removed by the package manager
 * as with module files, kernel object files aren't removed by the package manager


 * source code archives and distribution files for older versions of programs are not automatically removed when a new version is emerged
 * source code archives and distribution files for older versions of programs are not automatically removed when a new version is emerged


 * as with distribution files, binary packages are not automatically removed
 * as with distribution files, binary packages are not automatically removed

Per-user caching related files
Over time large (or a large number of) yet unnecessary files accumulate in certain directories as the system is used:


 * Cache folder used by browsers (and a few other applications)
 * Cache folder used by browsers (and a few other applications)


 * Folder to keep generated thumbnails
 * Folder to keep generated thumbnails

Filesystem reserved blocks percentage
On the ext* family of filesystems, 5% of all blocks are reserved for the privileged user and group by default when created. This provides a safety measure in the case of very low disk space, so privileged processes won't run out of disk space. However, on filesystems with hundreds of gigabytes, 5% is a lot more than would be typically needed in such a situation (on a 300 GB filesystem, that would be about 15 GB). Such reserved space would be even less useful on filesystems serving only as storage, e.g..

Filesystem fragmentation
Although most filesystems use strategies to prevent file fragmentation, some files get fragmented over time. A fragmented file may occupy more blocks than would be needed if the file was stored in a continuous way. Also, as free space becomes fragmented, the possibility of files becoming fragmented increases.

Removing system maintenance related files
The process or cleaning up these directories can be automated to a certain extent using these tools:


 * eclean in
 * removes files in and  intelligently, preserving files needed for rebuilding/repairing the system.


 * removes unused kernels and their files, preserving the most recent kernels
 * removes unused kernels and their files, preserving the most recent kernels

Reducing the reserved blocks percentage
The reserved blocks percentage on a ext* filesystem can be reduced using the tune2fs tool from the package.

This would reduce the percentage of reserved blocks on the filesystem to 1% on the partition represented by the block device :

Defragmenting the filesystem
This approach usually doesn't save much disk space, but might prevent further file fragmentation which would use up more disk space in the long run.

Several tools exist, some are filesystem specific (which usually provide better performance), some not:
 * defragments any filesystem in userspace by rewriting files to force continuous storage
 * defragments any filesystem in userspace by rewriting files to force continuous storage


 * e4defrag in
 * for the ext4 filesystem



Further resources
Several graphical tools exist that help to visualize occupied space on the filesystem which may help to identify directories and files that take up too much space:
 * KDE centric
 * KDE centric


 * KDE centric
 * KDE centric


 * GNOME centric
 * GNOME centric


 * not as visually pleasing but DE agnostic
 * not as visually pleasing but DE agnostic


 * terminal (ncurses) based
 * terminal (ncurses) based