Whilst Linux in general is very good at being neat and tidy, devices such as the Raspberry Pi, where resources can be much more limited than other computers, can sometimes suffer—usually from lack of space (performance will be dealt with in another post).
The Problem with Updates and Package Changes
If you’ve recently made changes—removed some packages, or manually updated some components—you may find your disk space getting a bit lower. This is because Linux package managers don’t always clean up perfectly after themselves. Some common scenarios that lead to wasted space include:
- Cached package files that were downloaded during updates
- Dependencies that are no longer needed after removing applications
- Older versions of packages kept after upgrades
- Temporary files created during installations
Simple Cleanup Commands
Fortunately, one of the tools you can use is built into apt-get
and allows it to scan and remove dependencies no longer required. To reclaim your disk space, simply run the following commands:
1. Clean Up Package Archives
sudo apt-get autoclean
This removes old archive files that might have been left behind. Note you can use clean
as well—however, this can cause slowness as it removes all archive files, and some may be needed again soon if you’re frequently installing and updating packages.
2. Remove Unused Packages
sudo apt-get autoremove
This removes any unused packages—often these can be left behind if you remove a programme which had optional dependencies, as these dependencies are not always removed by the system. Other system changes may have now marked certain packages as unused as well.
Results You Can Expect
The output of these commands will look similar to this:
Claiming back some disk space using apt-get autoremove
In this particular example, I gained back 300MB of space. However, I’ve previously recovered as much as 1.2 gigabytes from a Raspberry Pi that had gone through multiple update cycles and application changes.
When to Run These Commands
For optimal system maintenance, consider running these cleanup commands:
- After removing large applications
- Following significant system updates
- When you receive low disk space warnings
- As part of a regular monthly maintenance routine
- Before creating a backup or disk image
Additional Space-Saving Tips
Beyond these basic commands, here are some other ways to keep your Raspberry Pi’s storage optimised:
Clear Logs
Log files can accumulate over time. To safely truncate them:
sudo journalctl --vacuum-time=7d
This keeps only the last seven days of logs.
Check for Large Files
You can find unexpected space-hogging files with:
sudo find / -type f -size +100M
This will list all files larger than 100MB, helping you identify potential candidates for removal.
Minimise Installed Packages
Consider running a minimal installation if your Raspberry Pi is dedicated to a specific task. Many Raspberry Pi OS installations include software you might not need.
Conclusion
Regular maintenance using these simple commands can help ensure your Raspberry Pi runs efficiently within its storage constraints. This is particularly important for models with smaller SD cards or for projects that generate or download significant amounts of data.
By integrating these cleanup steps into your routine maintenance, you can avoid the frustration of running out of space unexpectedly and extend the useful life of your SD card by reducing unnecessary write cycles.