SD cards sizes are not (yet) unlimited. If you have a small SD card or are trying many things, you’ll probably run out of space quickly.
In this tutorial, I’ll show you how to find what takes up space on your SD card and how to remove it when possible.
Raspberry Pi OS includes useful tools, like Baobab on Desktop.
This application shows where are the largest files on the SD card, so that you can choose to keep or delete them, with the GUI or the command “rm <filename>”.
You’ll find two parts in this guide, one for each Raspberry Pi OS version (Desktop or Lite).
You can absolutely follow the Raspberry Pi OS Lite procedure on Desktop if you prefer.
Use the table of contents below to go directly to the parts that interest you.
Free disk space on Raspberry Pi OS Desktop
Find the large files
On Raspberry Pi OS Desktop, you can use a graphical tool to help you locate the largest files on the SD card (folders in fact).
This tool is named “Baobab” :).
- You can install it graphically by going into the Main Menu > Preferences > Add/Remove Software.
- Type “baobab” in the search engine on the left.
- Check the box corresponding to the Baobab package.
- Click “Apply” to install it.
Enter your password to confirm the installation.
That’s it! Baobab is ready to use.
Start and use Baobab (Disk Usage Analyzer)
The tool is now available in the Main menu:
- Open the software by clicking on System Tools > Disk Usage Analyzer.
- Select the folder to scan (probably /, so the second one).
- The software will scan your SD card and display the results like this:
- On the left, you can see a list of the folders in the selected path (/ in my example).
For each folder you have the total size in the second column. And you can click on the left arrow to browse inside each folder with the same display.
- On the right, there is a graphical visualization of the SD card usage.
- If you prefer, you can click on the icon under the graphic to switch to an alternate view:
- This allows you to locate the largest folders on your SD card.
- For example, in my case I see that opt is taking up over 1 GB, and I have no idea what it is.
- By opening this folder, I can see that “Wolfram” is installed.
If I don’t need it, I can uninstall it and save 1 GB.
Once you know what is taking too much space on your SD card, you can analyze it and remove any unnecessary files.
The first possibility is to remove files completely.
For example, you may have old files in your Downloads folder.
To do this, open the file explorer, go to Downloads and remove the unneeded files.
Don’t forget to empty the wastebasket on the desktop after doing this.
Note: If you don’t have the permission to remove a file, you can use the command I gave in the Raspberry Pi OS Lite part. Just open a terminal and type the commands.
It can also be that you install too many packages that you don’t eventually use.
For example, Wolfram in my example is a package installed by default on Raspberry Pi OS.
If you don’t need the files, you can remove it in the Add/Remove Software tool.
I already wrote a tutorial about this topic. If you need help, check it out here: How to uninstall programs on Raspberry Pi?.
That’s it for Raspberry Pi OS Desktop. If you have other cases or any issues, let me know in the comments and I will complete the tutorial.
Free disk space on Raspberry Pi OS Lite
On Raspberry Pi OS Lite, you don’t have a fancy graphical tool to find and remove files the same way:).
But you can use many commands that will often be faster to use, if you know how to use them.
Find the largest files
You have several commands that you can use when you start to run out of disk space.
I will give you four of them.
The first command you’ll probably need, is “df”.
df is a tool that you can use to display the disk summary.
It will show you a list of partition with several columns:
- Filesystem: basically the partitions, on Raspberry Pi you have generally / and /boot
- 1K-blocks: It represents the total size available on this partition
- Used: the disk space currently used
- Available: the available disk space
- Use%: the percentage of use
- Mounted on: the real location of the partition on the system
To get a more readable display, you can add the -h option like this:
If you want, you can also specify the partition, like this:
df -h /
Once you know what is the partition to analyze, you can go deeper, and check folder by folder.
To do this, you can use the “tree” command:
tree -dh --du
It’s not perfect. If you have many folders and subfolders, it can be difficult to find the biggest folder which may be an issue.
But, it’s a good tool that you can use once you know approximately where the problem is.
The next command may help you find the specific folder to analyze.
Find the biggest files in a specific folder
Ok, it’s a warrior command here, but it’s very useful.
I’m using it almost every week, and I’ve saved myself a lot of time since I’ve known it.
So, I’ll share it with you here.
“du” is a popular command on Linux to list all the files and folders in a specific folder.
The problem is almost the same as with “tree”, as you will need to scroll many files to find what you are looking for (and the display is not optimal).
Anyway, the idea here is to combine 3 commands:
- du: to display all the files and folder.
- sort: to sort the result by size.
- head: to display only the top 100 of the biggest files.
The command looks like this:
du -ak | sort -nr | head -100
And the result is perfect:
At the top, you have the biggest files and folders.
Then, they are sorted by size.
At a glance, you can see that Wolfram, Chromium and LibreOffice are taking a lot of space.
If you don’t need the files, uninstall them :).
Find files by names or sizes
Finally, the last command I use regularly is “find”.
Like the name suggests, “find” is a command to search for specific files your system.
Here are a two command options you can try:
- Search for files larger than X:
find <path> -size +<size>
find /home/pi -size +100M
- Search for files with a specific extension:
find <path> -iname *.<extension>
find /home/pi -iname *.mkv
As usual, use “sudo” if you want to search on the entire SD card.
Once you know which files are taking too much space, you can remove them if you don’t need them.
Remove the file
The first idea is to remove the files.
Basically, you can delete all the files in your user folder, if you don’t use them.
The command to do this is:
rm <file> or
rm -rf <folder>
Even if it’s not recommended removing system files like this, don’t forget to add sudo at the beginning to remove a file where you don’t have the permission.
Uninstall a package
The second option is to uninstall a package.
You can use apt to do this, for example:
sudo apt remove openjdk-11-jdk
If you have any question about this, you can check my tutorial on how to uninstall packages.
Clear the apt cache
And the last one is a bonus, that can often save you from a full SD card issue.
When your SD card is full, you can’t use most of the previous commands.
They need a minimum disk space to run (for example the “du” command uses a cache file to store all the results before sorting it by size).
What you can often try, is to remove the apt cache.
When you install a package, Raspberry Pi OS will download it locally before installing it.
Here is the command to remove all this temporary files:
sudo apt-get clean
And here is the result:
I just save 700M in two seconds :).
What to do if nothing works?
Generally, you will find a solution by using all the techniques and commands I gave you in this tutorial.
But sometimes, it’s just not possible to save enough space on the disk.
If you are using too much of your SD card, you may need to consider changing it to a bigger one.
You can find my recommendations on my recommend product page.
SD cards are cheap now. I’m using 32 and 64 GB SD card and I have no disk space issues on it.
If you are doing so, you may wonder how you can reinstall everything quickly?
If you are moving from an SD card of 2 GB to 32 GB for example, you can create an image of the 2 GB SD card and flash it on the 32 GB.
Then, expand the main partition to 32 GB, and you’ll quickly get the same system :).
I explain everything in this tutorial: How to back up and restore your Raspberry Pi (part 3).
That’s it! You now know how to solve your disk space issue on Raspberry Pi.
Most of the time, some cleaning will be enough to find a solution.
And in most complex situations you can always create an image of your SD card and flash it on a larger one.
I’m sure you’ll find a way :).
If you have any issue with this tutorial, or want to suggest other tips, feel free to leave a comment below.