Now if you’re like me, you’re bound to have a ton of VMs running with different Linux flavors and settings, but after some time, it tends to get cluttered and unnecessary data takes up all your allocated space for said Linux VM. I will show you a few easy tricks to help you clean up your system and free up storage when needed.
1 — Obtain disk information from a Linux system
2 — Inspect & delete individual folders
3 — Clean system data, logs, & package caches
Important Advice:
Please don’t run these commands blindly on live servers with production workloads.
Manually removing some temporary system files, caches, and logs might cause problems if there are serious programs actively accessing them.
As the title suggests, these commands are biased towards Debian-based Linux distributions (e.g. Ubuntu, Linux Mint), but the concepts will be common to all Linux distributions. In case you are on a different distro (e.g. CentOS/RHEL), please feel free to google and find the native commands for the distro of your choice.
Even though these commands work for Mint, it doesn’t mean that I endorse using Mint for servers. Mint is a beautiful Linux distro built for personal use. For servers, a light-weight/server version of Debian or Ubuntu is preferred.
1 — Obtain disk information
df: Check total disk size, mount locations, current usage, and free space
df is the most widely used tool to obtain disk information on the filesystem. Note that -h is used to print sizes in human-readable format (e.g. 2K, 50M, 16G).
df -Th
man df
lsblk: list block devices
lsblk is a great tool with some unique features such as Rotational Device detection (if ROTA:0 => HDD | if ROTA:1 => SSD). Note that -o is used to filter only the relevant columns and ignore the rest.
lsblk -o NAME,TYPE,SIZE,ROTA,MOUNTPOINT,MODEL
man lsblk
After observing the above results, you can easily identify which parts of the filesystem need more attention during the cleanup. Normally, old log files, build artefacts, and temporary application/user data take up considerable space and you can free up such space in the next stage.
2 — Inspect & delete individual folders
Inspect space used by folders
du is a widely used tool to inspect how much space is used by folders.
# Summarize file space usage (with directory depth of 1 level)
du -h --max-depth=1
# Summarize file space usage, sort in descending order, print top 10
du -h | sort -hr | head -n 10
# Explore more options
man du
Navigate to folders and delete unwanted data
If you use du and find big folders that require some cleanup, you can dig into those locations and remove unwanted data as follows.
# Change directory location
cd <directory_path>
# List content inside a directory
ls -alh
# Remove a file
rm <file_name>
# Remove a directory
rm -rf <directory_name>
3 — Clean system data, logs, & caches
Linux system processes and software packages installed on the server can also take up considerable space for storing various data such as logs, downloaded files, caches etc. Most such data can be deleted without any side effects to the system.
apt-get
As a software package management tool, apt-get has to maintain a lot of temporary data during package installs and upgrade operations. These data can be deleted safely.
# Delete all .deb files on /var/cache/apt/archives directory. Clean up the apt-get cache.
sudo apt-get autoclean
# Clean up downloaded .deb files from the local repository
sudo apt-get clean
# Remove packages that were automatically installed to satisfy dependencies for some package and no longer needed by those packages
sudo apt-get autoremove
# Remove unwanted software tools & packages
sudo apt-get remove package1 package2
logs — /var/log/*
/var/log/ is the default system log directory shared by Linux system processes and packages to store system logs. If they take large space, please feel free to delete them. Some tools even let users set log retention policies too — so that these logs will get automatically removed in the future.
# Clean old logs
cd /var/log
du -h | sort -hr | head -n 10
rm -rf .
syslogs
If syslogs is enabled on the server, /var/log/journal folder is likely to grow faster. As mentioned above, you may want to set a log retention policy as below.
# Set maximum syslogs size to 1GB and each log file size to 50MB
sudo vi /etc/systemd/journald.conf
[Journal]
SystemMaxUse=1G
SystemMaxFileSize=50M
# Set maximum syslog history to 2 days
sudo journalctl --vacuum-time=2d
# Set maximum syslog size to 100MB
sudo journalctl --vacuum-size=100M
# Restart syslog (journald)
sudo systemctl restart systemd-journald
/tmp
As the name suggests, /tmp folder is the default location to store temporary files. Most data here can be deleted safely. However, if you see any system-related content here, please double-check before cleaning them up.
# Delete temporary files
cd /tmp
rm -rf .
~/.cache
~/.cache folder is the default cache location for most tools. Feel free to check it and clean the content regularly.
# Clean cache directory
cd ~/.cache
du -h | sort -hr | head -n 10
rm -rf .
NPM Cache
Run the below commands to clean NPM cache (~/.npm/_cacache/).
# Clean NPM cache
npm cache clean --force
npm cache verify
These are the ones that I managed to find here and there and tested them on my own systems, so I know for sure they work as intended. I am suuuure there are plenty of other ways of doing this but please, have mercy on my newbie ways of fixing stuff.
I hope you all will find the above list of commands useful in your day-to-day work. If you can think of more approaches/suggestions to improve this list, I would love to hear them!
using GtkOrphan (orphaned packages) is also a great way, and also let’s not forget about removing old kernels.