How to Save Disk Space in Raspberry Pi OS and Purge Bloat

 Save Disk Space in Raspberry Pi OS
(Image credit: Shutterstock)

There is no getting away from bloat. Be it the middle aged spread or the ever increasing size of applications. But how can we battle the bloat on our Raspberry Pi? You could install the many lighter Raspberry Pi distros (Raspberry Pi OS Lite, or Diet Pi for example). But what if we want to reduce our running installation?

Linux uses package managers to install and remove software packages. Software is downloaded from official (or third party) repositories and the package manager handles the installation of the software and its dependencies. For Raspberry Pi OS, we use the Advanced Packaging Tool (APT) as the OS is a derivative of the Debian OS. Using the apt tools we can add and remove software packages, but how can we surgically remove the packages causing the bloat? Linux has the tools to identify and list the packages, ready for us to trim the fat.

We’re going to use two different methods to identify the largest applications / packages that bloat your Raspberry Pi installation. One comes preinstalled, the other is online one line of code away. For the test we performed a fresh install of the latest 32-bit Raspberry Pi OS image to a 16GB micro SD card.

Check the current size of the Raspberry Pi OS Install

1.  Open a terminal and use the df -h / command to check the filesystem.

df -h /

The df command is used to report the file system usage and has an extensive list of arguments to tailor the output to our needs. In this case we use the -h argument to format the output as “human readable”. The / instructs the command to look at the root filesystem of our drive, ignoring temporary files and ram disks.

2. Make a note of how much of the microSD card has been used. In our case we can see that our 16GB card (15GB available) has used 3.3GB, approximately 24% of the card for the OS install.

(Image credit: Tom's Hardware)

Cutting the Fat from Raspberry Pi OS

Finding the largest installed applications requires a little detective work and luckily Linux has the tools to do this. We can scan the database of installed applications and produce a list of the largest applications.

There are many ways to do this, but let's focus on two. The first uses the traditional tools, already installed on our Raspberry Pi. The dpkg-query command is used to query the installed applications and packages on our system.

1. Open a terminal.

2. Use the dpkg-query command to list the installed applications. There is a lot to pick from this command.
-W will list all the packages in a given pattern, a pattern that we shall later supply.
-f specifies the format of the output.
'${Installed-Size}\t${Package}\n' is the pattern and format we are searching for. In this case it is the size of the application, and the name. The installed-size value is given in bytes.

| sort -n -r will pipe the output of the query as the input of the sort command. Using -n and -r it will sort the returned list from largest to smallest.

| head -n 20 is another pipe, this time it pipes the reverse sorted list into the head command which will display the 20 largest applications.

dpkg-query -Wf '${Installed-Size}\t${Package}\n' | sort -n -r | head -n 20

3. Check the output of the command and look for any applications that can be removed. There are going to be a few large applications that we can remove. At the top of our list is the Chromium web browser (370439 bytes).

4. Use the purge command to remove the application / package from the OS. To illustrate the step we purged Chromium from our install. The purge command will also remove any configuration files and dependencies, so use with care and only if you have backups of any config files.

sudo apt-get purge -y chromium-browser

(Image credit: Tom's Hardware)

5. Press Y to continue. The purge will take a few moments.

6. Use the df -h / command to confirm that we now have extra free space.

df -h /

(Image credit: Tom's Hardware)

We gained 370MB of extra space on our micro SD card.

Using Aptitude to Save Disk Space in Raspberry Pi OS

The other way uses an application, aptitude, which will do all the hard work for us. Aptitude is a simplified package management tool that acts as an abstraction of the common apt-get and dpkg commands. 

1. Open a terminal and update the list of repositories.

sudo apt update

2. Install aptitude.

sudo apt install aptitude

3. List the top ten largest applications / packages. We sort the list via the size of each installed package and pipe the output to tail which will show the top ten packages. 

aptitude search "~i" --display-format "%p %I" --sort installsize | tail -10

4. Look down the list and locate a package that can be safely removed. Note that the list is sorted in ascending order of size. Again we chose the Chromium web browser.

(Image credit: Future)

5. Purge the package.  This is essentially the same as apt-get purge. Again we removed the Chromium Browser.

sudo aptitude purge chromium-browser

6. Press Y to continue. The purge will take a few moments.

7. Use the df -h / command to confirm that we now have extra free space.

df -h /

We gained 240MB of extra space on our micro SD card.

Both of these methods can be used to target and remove unwanted packages, but a word of caution. You may take a look down the list and spot raspberrypi-kernel (283MB), gcc-10  (40MB) or firmware (various) and think that these are valid targets for your spring cleaning. But do your research before culling the packages. Some will be dependencies for important applications and others will be important system files. A quick Google search will save your sanity and time. 

MORE: How to Set Up a Raspberry Pi for the First Time

MORE: How to Set Up a Headless Raspberry Pi

MORE: How to Run Linux on your Chromebook

Les Pounder

Les Pounder is an associate editor at Tom's Hardware. He is a creative technologist and for seven years has created projects to educate and inspire minds both young and old. He has worked with the Raspberry Pi Foundation to write and deliver their teacher training program "Picademy".

  • kenfiduk
    Admin said:
    Identify and remove the packages that bloat your Raspberry Pi OS installation. All you need are a few commands and a little Linux know-how.

    How to Save Disk Space in Raspberry Pi OS and Purge Bloat : Read more
    I am running Bullseye on my Raspberry Pi 4. When I attempt to install 'wajig', I get
    "
    Package wajig is not available, but is referred to by another package.
    This may mean that the package is missing, has been obsoleted, or
    is only available from another source

    E: Package 'wajig' has no installation candidate"
    Reply