sodcalculator

10 Tech How-To

Clean Lean Ubuntu: Maintenance Tips

Guide 10-52596 · Updated · Step by step

To keep an Ubuntu machine clean and lean, focus on removing unused packages, clearing cache files, and managing startup services. This article outlines practical steps for regular maintenance, covering package cleanup, disk usage analysis, log management, and system optimization.

Why Maintenance Matters

Ubuntu is generally self-cleaning, but over time, package caches, old kernels, and orphaned dependencies accumulate. This can fill up your disk, slow down boot times, and reduce available memory. A clean system not only runs faster but also reduces the risk of errors caused by low disk space. Regular maintenance helps you reclaim storage and keeps your system responsive, especially on machines with limited SSD capacity.

Cleaning Package Caches

The most significant space hog on many Ubuntu systems is the APT cache, which stores downloaded .deb files. After updates, these files are no longer needed. The command sudo apt clean removes all package files from /var/cache/apt/archives. A less aggressive option is sudo apt autoclean, which only removes files that can no longer be downloaded. To remove packages that were automatically installed as dependencies but are no longer needed, use sudo apt autoremove. This often frees several hundred megabytes after a major upgrade.

Removing Old Kernels

Ubuntu keeps the current kernel and one previous version for rollback safety. However, after many updates, old kernels can pile up. To see installed kernels, run dpkg --list | grep linux-image. You can safely remove older kernels with sudo apt purge linux-image-<version>, but it’s easier to use sudo apt autoremove --purge which will remove all kernels except the current one and the one before it. Always keep at least one older kernel in case the newest one fails to boot.

Clearing Systemd Journal Logs

Systemd journal logs can grow large over time. To check the current disk usage, run journalctl --disk-usage. To clear logs older than 7 days, use sudo journalctl --vacuum-time=7d. To limit the journal size to 100 MB, use sudo journalctl --vacuum-size=100M. For a permanent limit, edit /etc/systemd/journald.conf and set SystemMaxUse=100M, then restart the journal with sudo systemctl restart systemd-journald.

Finding Large Files and Directories

To identify what’s taking up space, use the du command. For example, du -h --max-depth=1 /home/yourname shows the size of each subdirectory. For a visual overview, install ncdu (sudo apt install ncdu) and run it in a terminal. It provides an interactive interface to browse large directories. Also check the ~/.cache folder, which often contains thumbnail caches and browser data. You can safely delete the contents of ~/.cache as it will be regenerated.

Managing Startup Applications

Fewer startup programs mean faster boot times. To see what starts at login, open “Startup Applications” from the system menu. Uncheck any programs you don’t need immediately, such as update notifiers or cloud clients. For system services, you can disable unnecessary ones with sudo systemctl disable <service>. Be cautious: only disable services you recognize and know are not essential. A common example is bluetooth if you never use it, but avoid disabling anything with “system” or “dbus” in the name.

Snap and Flatpak Cleanup

Snap packages store multiple versions of themselves. To remove old revisions, run sudo snap set system refresh.retain=2 to limit retained versions, then sudo snap remove --revision <revision> for each old one. To list revisions, use snap list --all. Flatpak users can run flatpak uninstall --unused to remove unused runtimes and extensions. Both tools can reclaim significant space if you have many apps installed.

Frequently Asked Questions

Is it safe to run sudo apt autoremove?

Yes, it is generally safe. It only removes packages that were installed as dependencies and are no longer required by any installed package. However, after a major release upgrade, it’s wise to review the list it shows before confirming, as it may remove packages you manually installed if they were marked as auto-installed.

How often should I clean my Ubuntu system?

Once a month is sufficient for most users. If you install and remove many packages frequently, you might want to do it weekly. The key is to check disk usage with df -h and see if you’re running low. There’s no harm in running the commands more often, but it’s unnecessary.

Can I delete files in /var/log?

Yes, but only the old compressed ones. Log files that are in use (like syslog or auth.log) should not be deleted manually. Use sudo journalctl --vacuum-time=7d for journal logs, and for other logs, consider using logrotate which is already configured to compress and rotate them. Deleting active log files can cause issues with services that have them open.

Why is my Ubuntu system slow even with low disk usage?

Low disk space isn’t the only cause. Check RAM usage with free -h and see if swap is being used heavily. Also, check for high CPU processes with top. Sometimes a single misbehaving application is the culprit. Also, consider if your system is using an HDD; defragmenting isn’t needed, but upgrading to an SSD is the biggest performance boost you can get.

Does cleaning improve battery life on laptops?

Indirectly, yes. Removing unnecessary startup services reduces background CPU usage, which can slightly extend battery life. Also, a cleaner system with fewer background processes means less disk I/O, which can help on systems with spinning disks. However, the effect is usually small compared to screen brightness and other power settings.

What is the difference between clean and autoclean?

sudo apt clean removes all .deb files from the package cache, regardless of whether they are still downloadable. sudo apt autoclean only removes files that can no longer be downloaded from the repositories, meaning it keeps the cache for packages that are still available. autoclean is safer if you want to keep some cache for offline reinstalls, but clean frees more space.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →