How to Save Disk Space in Raspberry Pi OS and Purge Bloat
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?
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.
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.
Cutting the Fat from Raspberry Pi OS
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
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 /
We gained 370MB of extra space on our micro SD card.
Using Wajig to Save Disk Space in Raspberry Pi OS
The other way uses an application, wajig, which will do all the hard work for us. Wajig 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 wajig.
sudo apt install wajig
3. Use wajig large to show all of the applications / packages larger than 10MB.
wajig large
4. Scroll 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, weighing in at 370MB.
5. Remove the package using wajig purge.
wajig purge chromium-browser
This is essentially the same command as apt-get purge, just wrapped in the simplified wajig command. Again we removed the 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 370MB of extra space on our micro SD card.
Read more: https://www.tomshardware.com/how-to/save-space-raspberry-pi-os
Looking to get your feet wet with Linux?