DEV Community

Cover image for How to free up disk space on Ubuntu Server
Alejandro Akbal
Alejandro Akbal

Posted on • Updated on • Originally published at blog.akbal.dev

How to free up disk space on Ubuntu Server

Introduction

So you have been running your Ubuntu Server for a while and recently found out that the disk usage is already at 70%!? Then lets free some space up.

This tutorial will help you liberate space on your system without breaking anything in the process.


Before we start

Preface

While this tutorial is focused on Ubuntu Server, it can be used for many other distributions that use the same packages, like Ubuntu Desktop, Debian, Linux Mint, etc.

Requisites

  • An Ubuntu server
  • Access to your server

Clean packages

Packages are archived and stored, if these versions can't be downloaded anymore --because there is a newer version or any other reason--, they end up being unnecessary. So lets clean lingering packages.

# Find no longer available packages and remove them
sudo apt autoclean -y
Enter fullscreen mode Exit fullscreen mode

Remove packages

Chances are that when you update and upgrade your system, some packages end up being unnecessary. But your system won't remove them, so lets tell it to do that.

# Find unnecessary or redundant packages and remove them
sudo apt autoremove -y
Enter fullscreen mode Exit fullscreen mode

Logs

Application logs keep increasing the disk usage of your server, specially if it is a busy one. But if we don't care much about keeping records, we can just delete them.

# Check current logs disk usage
sudo journalctl --disk-usage

# Rotate logs so they are saved to disk
sudo journalctl --rotate

# Clean any log that is older than one second
sudo journalctl --vacuum-time=1s

# One liner
sudo journalctl --rotate && sudo journalctl --vacuum-time=1s
Enter fullscreen mode Exit fullscreen mode

Biggest files

Now we are switching to a more manual approach, lets find out what the biggest files on our system are.

# Find biggest files in "/" and show their size in human readable format
sudo du -a -h /

# Sort the output
sort -n -r

# Show only the top 15 results
head -n 15

# Combined in a one liner
sudo du -a -h / | sort -n -r | head -n 15
Enter fullscreen mode Exit fullscreen mode

And then delete them

# Delete a file
sudo rm /path/to/file
Enter fullscreen mode Exit fullscreen mode

⚠ Be careful to not delete any important file, in case of doubt, don't do it. ⚠


End

What next?

You can now search for more specific guides.

For example, if you are using Docker, you might want to learn how to remove unnecessary resources.

Self promotion

If you have found this useful then you should follow me, I will be posting more interesting content! 🥰

Or support me financially. 💸

Conclusion

Today you have learned how to free up space on your system by removing packages, logs and files.

Let me know how much space you have recovered in the comments!

Top comments (3)

Collapse
 
johnbetong profile image
John Betong

I recently checked my disk usage and was horrified to discover that MySql had nearly 5Gb of log files!

This is no doubt due to completely deleting tables and updating them from an external three XLS Spreadsheet - every hour.

BEWARE:
It is not straight forward how to delete the log files and care must be taken

Collapse
 
alejandroakbal profile image
Alejandro Akbal

What, those were serious log files, glad you took care of them!

Collapse
 
pazyp profile image
Andrew Pazikas

Another good command to list to 10 largest files by size, can be really useful for quickly finding large logs across a whole server.

find / -type f -exec ls -h {} \; |awk '{print $7, $11}' | sort -n | tail -10

chnage the tail value to show more files :)