Spring-cleaning your (Arch) Linux system

Disclaimer: Some operations mentioned in this post are potentially destructive and irreversible. Be sure to back up all your important data before proceeding.

Note: This post is written from the point of view of an Arch Linux user. Most steps presented below should nevertheless translate well to other distributions.

Through the usual course of their operation, operating systems (even Arch Linux) tend to slowly accumulate obsolete data. In most cases, this is not a problem. However, if you are like me, it gives you a nice and warm feeling to have a clean system. Apart from that, keeping your file system clean will also help you save some disk space and reduce the duration of system upgrades. More importantly, it will soon make you an expert of your operating system.

pacreport is a utility that lists (possibly) obsolete packages and files on your system. You can get it by installing the pacutils package. The following magic command will run pacreport, reformat its ouput and pipe the result into several files for easier post-editing:

sudo pacreport --unowned-files | head -n -2 | awk '$1 ~ /^[A-Z]/ {print $0} $1 !~ /^[A-Z]/ {print $1}' | csplit -szf pacreport - /:$/ {*}

The command should leave you with five files named pacreport0*. These files will help us in removing the following categories of obsolete data:

  1. Obsolete packages
  2. Obsolete system-level files
  3. Obsolete user files

Uninstall obsolete packages

Packages become obsolete for at least two reasons. For one, you might simply not need them anymore (unneeded packages). And secondly, they might have been installed as dependencies for other packages that are long gone (orphaned packages).

The pacreport command has generated three package lists for us: pacreport01, pacreport02 and pacreport03. Each of these lists potentially contains unneeded or orphaned packages. Now it’s your turn to go through these lists and leave only those packages you would like to remove. If you are unsure about some package, use the pacman -Qi some-package command to get more information. In case you would like to keep a package listed in pacreport02, remove it from the file and mark it as explicity installed:

sudo pacman -D --asexplicit some-package

Once you are done, remove the files’ header lines and run the following command:

sudo pacman -Rscn $(cat pacreport01 pacreport02 pacreport03)

After double-checking the output, confirm the removal operation to finally remove the listed packages. In my case, more than 400 packages were removed in this way.

Remove obsolete system-level files

Many administrative processes store data all across the filesystem. For example, pacman stores downloaded packages in /var/cache/pacman/pkg/ but does not remove them automatically. In case of problems after an upgrade, this practice allows downgrading a package without the need to re-download an older version. On the other hand, this directory can grow very large in size if not pruned periodically.

The paccache script that comes with the pacman-contrib package deletes all cached package versions except for the three most recent:

sudo paccache -r

To (additionally) remove all cached versions currently not installed execute the pacman -Sc command. In this way, you remove all cached versions of packages not currently installed and only retain the three most recent versions of all installed packages.

But it is not only pacman spreading files across the filesystem. Any process with sufficient permissions can create files wherever it likes. As these files are not part of the original distribution package, they are not automatically removed when uninstalling a package.

Luckily, the above pacreport command has also generated a list of unowned files. Open pacreport00 and go through the list of files, removing only those file paths you would like to keep. /etc/pacreport.conf allows you to track unowned-but-needed files and their associations (run man pacreport for an example). Then, when using pacreport --unowned-files, the files referenced in /etc/pacreport.conf will be omitted. Finally, remove the files left in pacreport00 with the below command:

sudo rm -r $(cat pacreport00)

Remove obsolete user files

Removing personal files in /home is often the most labour-intensive step as it can’t be automated easily. This is because any process a user executes can create any file within that user’s home directory. Fortunately, the process of manually removing obsolete files from your /home directory isn’t as tedious as it might sound at first.

My method of choice is performing a manual depth-first traversal of my /home directory tree, evaluating the files & directories I encounter and, if appropriate, removing them. Pay special attention to the following directories:

  • ~/.config/ – default directory for application configuration files
  • ~/.cache/ – default user cache directory
  • ~/.local/share/ – also used for application-specific configuration files