How to clean Ubuntu safely

Back to the home page

As the Victorians used to say: cleanliness is next to godliness. So here are some tips to make your Ubuntu 18.04 even more divine.

First of all: never use cleaning applications like BleachBit! Those software wrecking balls are very risky and may damage your system beyond repair. There are a few safe cleaning actions, which I'll describe below.

Note: this is the how-to for cleaning Ubuntu. The how-to for cleaning Linux Mint is here.

Ubuntu doesn't get polluted much over time (with one notable exception, namely old kernels. More about that later). It even doesn't need defragmentation. The only cleansing actions you might want to do in Ubuntu, are the following:

Clear the updates cache

1. First of all, use the application Software to install Synaptic Package Manager.

Then, on the bottom left, click on the big square button with the rows of white dots (Show Applications). Query: synaptic

Click on Synaptic Package Manager

Settings - Preferences - Files

Put the dot at: Delete downloaded packages after installation

Press the button: Delete cached package files

Clear the thumbnail cache

2. For each shown picture, Ubuntu automatically creates a thumbnail, for viewing in the file manager. It stores those thumbnails in a hidden directory in your user account (names of hidden directories and hidden files start with a dot, like .cache or .bash_history. The dot makes them hidden).

Over time, the number of thumbnails can increase dramatically. Moreover, the thumbnail cache will eventually contain many superfluous thumbnails of pictures that don't exist anymore.

My advice is therefore, to clear the thumbnail cache every six months or so. The quickest way is to use the terminal:

Launch a terminal window.
(You can launch a terminal window like this: *Click*)

Type (use copy/paste to avoid errors):

rm -v -f ~/.cache/thumbnails/*/*.png ~/.thumbnails/*/*.png

Press Enter.

Followed by:

rm -v -f ~/.cache/thumbnails/*/*/*.png ~/.thumbnails/*/*/*.png

Press Enter.

Note: this will probably affect the thumbnails on your desktop as well; in that case it should suffice to simply log out and in again (or reboot your computer), which will create them anew.

Repeat the above in each user account.

You can permanently limit the size of the thumbnail cache as follows:

Note: for this, you have to install the package dconf-editor first.

Launch a terminal window.
(You can launch a terminal window like this: *Click*)


Press Enter

In dconf-editor, click on the small triangle to expand a category. Like this, click your way to:
org - gnome - desktop - thumbnail_cache
.... and set the maximum-age to 90 (press Enter) and the maximum-size to 64 (press Enter again).

Close dconf-editor.

Repeat this in each user account. Afterwards, you won't have to pay attention to the disk space of the thumbnails anymore.

Want to get rid of polluted settings in your web browser?

3. Do you have polluted settings in Firefox, Chrome or Chromium (sometimes caused by rotten, shady or rogue add-ons), and do you wish to start anew with a clean browser? Then proceed like this:

a. First make a backup of your current web browser settings (because you never know why you might need them sometime):

- Launch a terminal window (this is how to launch a terminal window: *Click*).

- Use copy/paste to transfer the following green command line to the terminal:

For Firefox:
cp -r -v ~/.mozilla ~/.mozillabackup

Press Enter.

For Chrome:
cp -r -v ~/.config/google-chrome ~/.config/google-chromebackup

Press Enter.

For Chromium:
cp -r -v ~/.config/chromium ~/.config/chromiumbackup

Press Enter.

b. Now export your bookmarks to a backup file:

For Firefox:
Click the Bookmarks button (the one on the right of the little star) - Show All Bookmarks

Import and Backup - Backup...

Save the bookmarks-xxx.json file to the location you prefer.

Later on, you can import your bookmarks again in a clean Firefox.

For Chrome / Chromium:
On the upper right in your browser window, click on the three dashes - Bookmarks - Bookmark manager

Click on Organize - Export bookmarks to HTML file...

Later on, you can import them again in your clean Chrome / Chromium.

c. You will also lose all of your stored login passwords for websites! Make sure you know them all.

d. Close the web browser you wish to clean.

e. Launch a terminal window (this is how to launch a terminal window: *Click*).

f. Copy/paste this green command line into the terminal:

For Firefox:
rm -r -v ~/.mozilla && rm -r -v ~/.cache/mozilla

Press Enter.

For Chrome:
rm -r -v ~/.config/google-chrome && rm -r -v ~/.cache/google-chrome

Press Enter.

For Chromium:
rm -r -v ~/.config/chromium && rm -r -v ~/.cache/chromium

Press Enter.

g. Launch your web browser again. It should be clean.

h. Import your old bookmarks from the backup you've created. Importing can be done by means of the same feature as the one you've used for exporting.

You're done! From now on, avoid all shady add-ons and extensions, and install only those that you really need and trust.

(continued in the column on the right)

This website is being sponsored by Google Ads.

Are you using an ad blocker? Then you're also blocking my earnings from advertisements....

If you wish to support my website, you can configure your ad blocker to make an exception for this website. Or you can make a donation (and get free goodies).

Thanks in advance....

The registry

4. There's no need to clean the registry of Linux, as it can't get polluted in the first place. For the following reasons:

- Only the operating system itself has a central registry. The configurations of the applications aren't in there, because they don't have access to it. So they can't mess it up. They place their own default settings in their own folders in the system.

- applications place upon installation a hidden settings file in the personal folder of each user. That's the only settings file that a user has access to. More or less like MS-DOS did, when each application only created its own .ini file with its settings.

- each user has his own hidden copy of the central registry in his personal folder. That copy is the only thing that he can mess up, not the registry of another user account.

Remove old kernels

5. Unlike Linux Mint, Ubuntu gets kernel updates by default. This happens regularly. After a kernel update, the old kernel still shows in the Grub boot menu, under the header: "Advanced options for Ubuntu". Because you might want to start your machine with the old kernel, if the new kernel doesn't function well.

So far, so good. But having more than one or two redundant kernels, is superfluous and a waste of disk space: each kernel uses up a considerable amount of space. This is how you can quickly remove old kernels and thereby clean up the Grub boot loader menu as well:

a. First download a script that I've forked from a script of the byobu package called "purge-old-kernels": purge-old-kernels-2 (note for the gurus: I've also hosted it on GitHub).

If you wish, you can inspect what it does by opening it with your text editor. But it's important that you leave it in the folder Downloads.

b. Launch a terminal window.
(You can launch a terminal window like this: *Click*)

c. For moving the script into the right system folder, copy/paste the following command line into the terminal, in order to avoid typing errors:

sudo mv -v ~/Downloads/ /opt

Press Enter. Type your password when required: your password will remain entirely invisible, not even dots will show when you type it, this is normal. Press Enter again.

d. Then copy/paste this line into the terminal, which will set the correct permissions for the script:

sudo chmod -v 755 /opt/

Press Enter.

e. Launching the script will remove all kernels except your current active kernel and the latest old one (it's a good idea to keep one spare kernel, so that's OK). Launch it with this terminal command (use copy/paste to transfer it into the terminal):

sudo sh /opt/

Press Enter.

Simply wait until the job has finished.

f. Finally, copy/paste this command line into the terminal:

sudo rm -v /boot/*.old-dkms

Background for this final cleaning command: it's often not necessary, in which case it'll report that it can't find any file to remove. But when DKMS builds a module for the kernel, it can also include it in the initramfs (the initrd files you find in /boot).

Before doing so, DKMS creates a backup of the current initramfs, adding .old-dkms at the end of the file name.

So, unless DKMS crashes while generating the initramfs (quite unlikely), the .old-dkms files are of no use afterwards. In certain circumstances they can take up quite some space, so out with them.

Finished! That's all you ever need to do. Doing more is risky and not advisable.

Note: don't use cleaning applications like Bleachbit and Computer Janitor for this job. They are dangerous and at best superfluous.

Prevent future disk pollution and pin Ubuntu to a kernel version

5.1. Kernel updates are rarely relevant for ordinary desktop users. That's even true for security updates for kernels, while kernel updates do carry the risk of damaging the stability of your system. That's why they're being treated with extra caution in Linux Mint, for example.

So you might consider to disable kernel updates in Ubuntu, by pinning it to its current kernel version. Both for the stability of your system and for preventing future disk pollution. You can do that as follows (item 7, left column).

Make Firefox cleanse itself automatically upon quitting

6. Improve your privacy: you can configure Firefox to cleanse itself automatically, upon quitting. All cookies and history are being deleted then. Furthermore, you can limit the tracking that some websites do to follow you.

The price you pay is a small decrease in user friendliness, but it's not much. The privacy gain is huge, and outweighs this price by far.

You can do it like this:
Panel of Firefox - Edit - Preferences - tab Privacy

a. Item Tracking: set it to:
Tell sites that I do not want to be tracked

b. Item History: change the setting to:
Firefox will: Use custom settings for history

c. Item Cookies: change the setting to:
Keep until: I close Firefox

d. Now tick the following setting:
Clear history when Firefox closes

e. Finally, click the button "Settings..." and tick everything, except for Saved Passwords and Site Preferences. Click OK.

Click Close and you're done.

Tip: sometimes it may come in handy to force a cleansing during your web browsing. Simply by closing Firefox and launching it anew.

Remove most Asian fonts

7. If you're not a user of Asian fonts, you might remove a couple of those. That should free up several hundred MB's of disk space, but more importantly: the font selection box in Libre Office will become much less cluttered.

Note: sometimes, removing fonts may have unwanted side effects! Although I haven't experienced those on my machines yet after the removal of the Asian fonts described below, it's something to keep in mind....

This is how to remove most Asian fonts:

a. Launch a terminal window.
(You can launch a terminal window like this: *Click*)

b. Copy/paste the following command line into the terminal, in order to avoid typing errors. It's one huge line:

sudo apt-get remove fonts-kacst* fonts-khmeros* fonts-lklug-sinhala fonts-guru-extra fonts-nanum* fonts-noto-cjk fonts-takao* fonts-tibetan-machine fonts-lao fonts-sil-padauk fonts-sil-abyssinica fonts-tlwg-* fonts-lohit-* fonts-beng-extra fonts-gargi fonts-gubbi fonts-gujr-extra fonts-kalapi fonts-lohit-* fonts-samyak* fonts-navilu fonts-nakula fonts-orya-extra fonts-pagul fonts-sarai fonts-telu* fonts-wqy* fonts-smc* fonts-deva-extra

Press Enter. Your password will remain entirely invisible, not even dots will show when you type it, that's normal. Press Enter again.

c. Just to make sure, follow it up with this terminal command:

sudo dpkg-reconfigure fontconfig

Press Enter.

d. Reboot your machine.

Finally: I strongly advise to leave it at that. Don't remove any other fonts, because of the aforementioned risk of negative side effects!

How to undo: re-install removed Asian fonts

7.1. Regrets? If you want to re-install the Asian fonts that you've removed by applying the how-to in item 7, simply replace the word "remove" by "install" in its removal command line. Run dpkg-reconfigure fontconfig again, reboot and all should be like it was before.

Want more tips?

8. Do you want more tips and tweaks for Ubuntu? There's a lot more of them on this website! For example 10 fatal mistakes that you'll want to avoid in Ubuntu.

To the content of this website applies a Creative Commons license.