Keeping your system tidy helps free disk space and can improve stability. This guide shows step-by-step terminal commands to clean up Ubuntu, remove unused packages, reclaim space from caches and old kernels, and verify results. Follow the Prerequisites → Installation → Setup → Verification → Troubleshooting sequence to safely clean your system.
Prerequisites
Before you begin, make a quick backup (or snapshot) of important data and make sure you have a user with sudo privileges. Updating the package cache first ensures the package manager operates with current metadata.
Update apt cache
sudo apt update
Hit:1 http://archive.ubuntu.com/ubuntu jammy InRelease Reading package lists... Done Building dependency tree Reading state information... Done All packages are up to date.
Explanation: sudo runs the command as root. apt update refreshes package lists — a safe first step.
Installation (Tools you may want)
We install a few tools that help with deeper cleanup: deborphan to find orphaned libraries and bleachbit for a GUI/CLI deep clean.
Install deborphan
sudo apt install deborphan -y
Reading package lists... Done Building dependency tree Reading state information... Done The following NEW packages will be installed: deborphan 0 upgraded, 1 newly installed, 0 to remove and 12 not upgraded. Need to get 34.5 kB of archives. After this operation, 139 kB of additional disk space will be used. Get:1 http://archive.ubuntu.com/ubuntu jammy/universe amd64 deborphan amd64 1.0.37 [34.5 kB] Fetched 34.5 kB in 0s (110 kB/s) Selecting previously unselected package deborphan. (Reading database ... 210245 files and directories currently installed.) Preparing to unpack .../deborphan_1.0.37_amd64.deb ... Unpacking deborphan (1.0.37) ... Setting up deborphan (1.0.37) ... Processing triggers for man-db (2.10.2-1) ...
Explanation: deborphan finds libraries that are no longer required by any installed package.
Install BleachBit (optional)
sudo apt install bleachbit -y
Reading package lists... Done Building dependency tree Reading state information... Done The following NEW packages will be installed: bleachbit 0 upgraded, 1 newly installed, 0 to remove and 12 not upgraded. Need to get 3,210 kB of archives. After this operation, 12.5 MB of additional disk space will be used. Get:1 http://archive.ubuntu.com/ubuntu jammy/universe amd64 bleachbit amd64 4.0 [3,210 kB] Fetched 3,210 kB in 1s (4,075 kB/s) Selecting previously unselected package bleachbit. (Reading database ... 210315 files and directories currently installed.) Preparing to unpack .../bleachbit_4.0_amd64.deb ... Unpacking bleachbit (4.0) ... Setting up bleachbit (4.0) ... Processing triggers for desktop-file-utils (0.24-1ubuntu3) ...
Explanation: bleachbit offers both GUI and CLI cleaning options (caches, temp files, application data). Use with care — it can remove saved data.
Setup — What to Clean
We will cover these main cleanup areas: package leftovers, package cache, old kernels, snap packages, logs, orphaned libraries, thumbnails, and large folders. Each command below is followed by a sample output and a short explanation.
1) Remove unused packages and dependencies (apt)
After uninstalling software, dependencies can remain. Use:
sudo apt autoremove
Reading package lists... Done Building dependency tree Reading state information... Done The following packages will be REMOVED: libfoo1 libbar2 0 upgraded, 0 newly installed, 2 to remove and 12 not upgraded. After this operation, 8,765 kB disk space will be freed. Do you want to continue? [Y/n] y (Reading database ... 210320 files and directories currently installed.) Removing libfoo1 (1.2.3-4) ... Removing libbar2 (2.3.4-1) ... Processing triggers for man-db (2.10.2-1) ...
Explanation: apt autoremove removes packages installed as dependencies that are no longer required. It’s safe to review the list before confirming.
2) Remove old package files from apt cache
Two commands help: apt autoclean and apt clean.
sudo apt autoclean
Reading package lists... Done Building dependency tree Reading state information... Done Autoclean: 12 cached packages removed.
Explanation: apt autoclean deletes package files that can no longer be downloaded (obsolete).
sudo apt clean
Explanation: apt clean removes all files in the local package cache (usually /var/cache/apt/archives). The output is typically empty on success.
3) Remove a specific package with dpkg
To remove a package but keep configuration files:
sudo dpkg --remove
(Reading database ... 210300 files and directories currently installed.) Removing (1.4.5-2) ... Processing triggers for man-db (2.10.2-1) ...
To purge a package including configuration:
sudo dpkg --purge
(Reading database ... 210300 files and directories currently installed.) Purging configuration of (1.4.5-2) ... Processing triggers for man-db (2.10.2-1) ...
Explanation: dpkg operates at a lower level than apt. Use --purge to remove config files too. Prefer apt remove / apt purge for dependency-aware removal.
4) Remove old kernels
First confirm your current kernel — don’t remove it.
uname -r
5.15.0-70-generic
Explanation: uname -r prints the running kernel version. Replace linux-image- below with versions you are NOT running.
sudo apt --purge remove linux-image-5.15.0-68-generic
Reading package lists... Done Building dependency tree Reading state information... Done The following packages will be REMOVED: linux-image-5.15.0-68-generic 0 upgraded, 0 newly installed, 1 to remove and 12 not upgraded. After this operation, 50.3 MB disk space will be freed. (Reading database ... 209900 files and directories currently installed.) Removing linux-image-5.15.0-68-generic (5.15.0-68.76) ... update-initramfs: Deleting /boot/initrd.img-5.15.0-68-generic Processing triggers for initramfs-tools (0.140ubuntu1) ...
Explanation: Removing old kernel packages frees space in /boot. Always keep at least one older kernel as a fallback and never remove the currently running kernel shown by uname -r.
5) Clean up Snap packages
List installed snaps, then remove unneeded ones.
snap list
Name Version Rev Tracking Publisher Notes core 16-2.58.3.1 14050 stable canonical✓ core vlc 3.0.16 2400 stable videolan✓ -
sudo snap remove vlc
vlc removed
Explanation: snap packages are self-contained; they can occupy significant space. Use snap list to inspect them and sudo snap remove <snap_name> to delete.
6) Rotate and vacuum systemd journal logs
To remove logs older than a time period:
sudo journalctl --vacuum-time=7d
Vacuuming done, freed 128.0M of archived journals from /var/log/journal/abcdef1234567890
To limit journal size to a maximum:
sudo journalctl --vacuum-size=100M
Vacuuming done, reduced journal size to under '100M'
Explanation: journalctl --vacuum-time and --vacuum-size prune old systemd journal logs to free disk space. Choose sensible values based on your need for historical logs.
7) Find and remove orphaned libraries (deborphan)
deborphan
libgtk-3-0 libfoo-gtk libbar-qt
sudo apt-get remove --purge $(deborphan) -y
Reading package lists... Done Building dependency tree Reading state information... Done The following packages will be REMOVED: libgtk-3-0 libfoo-gtk libbar-qt 0 upgraded, 0 newly installed, 3 to remove and 12 not upgraded. After this operation, 9,012 kB disk space will be freed. (Reading database ... 210200 files and directories currently installed.) Removing libgtk-3-0 (3.24.30-1) ... Removing libfoo-gtk (1.2.1-3) ... Removing libbar-qt (0.9.8-2) ... Processing triggers for libc-bin (2.35-0ubuntu3) ...
Explanation: deborphan lists orphaned libraries; passing its output to apt-get remove --purge removes them. Review the list before removing to avoid accidentally deleting libraries needed by manually installed software.
8) Clean thumbnails cache
Thumbnails can accumulate under your user cache. Remove them:
rm -rf ~/.cache/thumbnails/*
Explanation: Removing cached thumbnails reclaims space; the cache will rebuild as needed. This command typically produces no output.
9) Inspect disk usage to find large folders (du)
du -h --max-depth=1 ~
1.2G /home/youruser/Videos 654M /home/youruser/.cache 120M /home/youruser/.local 3.5G /home/youruser
Explanation: du shows disk usage. -h prints human-readable sizes; --max-depth=1 limits output to top-level folders in your home directory so you can target large directories for cleanup.
10) Deep cleanup with BleachBit (optional)
sudo bleachbit --list
BleachBit 4.0 Available cleaners: apt_cache thumbnail_cache firefox.cache system.tmp
sudo bleachbit --clean apt_cache,thumbnail_cache,system.tmp
Cleaning apt_cache... Done Cleaning thumbnail_cache... Done Cleaning system.tmp... Done BleachBit completed successfully.
Explanation: BleachBit can run in CLI mode to clean many application caches and temporary files. Always review which cleaners will run; some options remove saved sessions, cookies, or other data you may want to keep.
Verification
After cleanup, re-check free space and confirm that important services still run.
Check available disk space
df -h /
Filesystem Size Used Avail Use% Mounted on /dev/sda1 100G 62G 34G 65% /
Explanation: df -h shows disk usage for mounted filesystems in human-readable format.
Double-check running kernel
uname -r
5.15.0-70-generic
Explanation: Confirm you did not remove the running kernel.
Confirm no critical services were removed
systemctl --failed
UNIT LOAD ACTIVE SUB DESCRIPTION 0 units listed.
Explanation: systemctl --failed lists units with problems. An empty list indicates services are fine. Note: For commands like systemctl, output will vary depending on system status.
Troubleshooting
I removed something important by accident — how to recover?
If you removed a package you need, reinstall it with apt. If you removed a kernel you require but still have another installed, run:
sudo apt install linux-image-$(uname -r)
Reading package lists... Done Building dependency tree Reading state information... Done linux-image-5.15.0-70-generic is already installed and configured.
If a service fails after removal, check logs with journalctl -u <service-name> and reinstall the package if necessary.
Deborphan removed too much
Always inspect deborphan output before piping it into removal. If an essential package was removed, reinstall it:
sudo apt install <package_name>
Reading package lists... Done Building dependency tree Reading state information... Done The following NEW packages will be installed: <package_name> 0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded. Need to get 2,345 kB of archives.
BleachBit deleted something you needed
BleachBit performs permanent deletions. If you have backups or Timeshift snapshots, restore from them. If not, check if the removed data can be recreated (browser data, caches). For future runs, use BleachBit's preview mode or run only specific cleaners.
Quick cleanup checklist
- sudo apt update
- sudo apt autoremove
- sudo apt autoclean && sudo apt clean
- Remove unwanted snaps:
sudo snap remove <snap_name> - Prune logs:
sudo journalctl --vacuum-time=7dor--vacuum-size=100M - Remove thumbnails:
rm -rf ~/.cache/thumbnails/* - Find orphaned libs:
deborphanand remove cautiously - Inspect large folders with
du -h --max-depth=1 ~and clean as needed
Conclusion
Regular maintenance using these terminal commands helps you clean up Ubuntu and free disk space while keeping the system tidy and stable. The commands above — from apt autoremove to journalctl –vacuum-time and deborphan — are effective when used carefully. Always verify with df, du, and systemctl --failed after cleanup, and keep backups or snapshots in case you need to restore removed data.