How to Free Disk Space on Raspbian? (Desktop or Lite)

free disk space on raspberry pi

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

How to Free Disk Space on Raspbian?
The first step is to identify exactly where are the largest files that take up space on your disk. Raspbian offers many tools like Baobab on Desktop.
Then you can choose to keep or delete them, with the command “rm <filename>”.

This guide will be separated in two parts depending on the Raspbian version you use (Desktop or Lite)
You can absolutely follow the Raspbian Lite procedure on Desktop if you prefer
Use the table of contents below to go directly to the part that interests you

Free disk space on Raspbian Desktop

Find the large files

On Raspbian 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” πŸ™‚

Install 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 way you can locate exactly 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

Remove them

Once you know what is taking too much space on your SD card, you can analyze it and remove the unnecessary things

Remove 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 give in the Raspbian Lite part. Just open a terminal and type the commands

Uninstall packages

It can also be that you install too many packages that you don’t use eventually
For example, Wolfram in my example is a package installed by default on Raspbian.
If you don’t need them, you can remove it in the Add/Remove Software tool

I already write a tutorial about this if you need help, check it here: How to uninstall programs on Raspberry Pi?

That’s it for Raspbian 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 Raspbian Lite

On Raspbian Lite, you don’t have a fancy graphical tool to do it 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 lack of disk space
I will give you four of them

Disk usage

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 /

Analyze sub-folders

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

Don’t forget to use “sudo” in system directories

It’s not perfect. If you have many folders and subfolders, it’s not easy to find the biggest folder that may be the issue
But it’s a good tool that you can use once you know approximately where is the problem

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 very useful
I’m using it almost every week, and I save many times since I know 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 look like this:
du -ak | sort -nr | head -100

And the result is perfect:

You can use it on the entire partition
Same thing, you need to use sudo to get all the information

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 them, 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 options you can try:

  • Search for files larger than X:
    find <path> -size +<size>
    Example: find /home/pi -size +100M
  • Search for files with a specific extension:
    find <path> -iname *.<extension>
    Example: find /home/pi -iname *.mkv

As usual, use “sudo” if you want to search on the entire SD card

Remove them

Once you know what are the files 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 use 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, Raspbian 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 asking too much to your SD card, you may need to consider changing it by a bigger one
You can find my recommendations on my recommend product page
SD card are cheap now. I’m using 32 and 64 GB SD card and I have no disk space issue on it

If you are doing so, you may wonder how you can reinstall everything quickly?
If you are moving from a SD card from 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)

Conclusion

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

How useful was this post?

Click on a star to rate it!

Average rating 4 / 5. Vote count: 1

No votes so far! Be the first to rate this post.

As you found this post useful...

Spread the word!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Leave a Reply

Your email address will not be published. Required fields are marked *