Script to clean up Ubuntu OS

×

Overview

module url N/A
git repository https://bitbucket.org/arrizza-public/proj-man-os-cleaner
git command git clone git@bitbucket.org:arrizza-public/proj-man-os-cleaner.git
verification report https://arrizza.com/web-ver/proj-man-os-cleaner-report.html
version info
OS Language #Runs Last Run Cov%
Ubuntu 24.04 noble Python 3.12 264 2025-12-05 82%

Summary

This project has a python script that cleans up the Ubuntu disk image:

  • removes Trash
  • cleans up old kernels
  • removes specific apps. Currently:
    • thunderbird
    • freecad
  • cleans up logs in /var/log tree
  • cleans up any snap packages that are disabled
  • cleans up the apt caches
  • cleans up journal logs
  • cleans up removed apt packages that still have configuration data (marked "rc")
  • reports other possible packages/snaps to clean up

Please note: this script works on my installation of Ubuntu 24.04. Using it on your PC may cause issues. It is unlikely, but there are many variants and configurations for all of Ubuntu installations.

run

  • do these only once
./do_subm_update   # to install common support scripts
./do_install full  # to install byobu
  • If you DO NOT WANT to delete thunderbird and freecad, comment these out in lib/app.py see run() around line 30.
  • run this to clean the disk drive
./do-ubu-cleaner

Typical output:

==== do-ubu-cleaner: starting...
     initial usage: 343.7 GB
==== trash/cache cleanup
==== cleanup kernels
 --    1] Reading package lists...
 --    2] Building dependency tree...
 --    3] Reading state information...
 --    4] 0 upgraded, 0 newly installed, 0 to remove and 11 not upgraded.
==== removing thunderbird
     package thunderbird not found in snap or dpkg
==== removing freecad
     package freecad not found in snap or dpkg
==== cleanup logs
 --    1] Vacuuming done, freed 0B of archived journals from /run/log/journal.
 --    2] Vacuuming done, freed 0B of archived journals from /var/log/journal/044ac0ec7e60409091ad0d684542ffa1.
 --    3] Vacuuming done, freed 0B of archived journals from /var/log/journal.
     found 0 log files
==== checking for snap packages that are disabled
     found 0 disabled packages
==== cleanup apt caches
==== cleanup dpkg removed packages
     found 0 removed packages
==== report:
     These are the top 10 largest apt packages:
       4916763	kicad-packages3d
       526657	linux-firmware
       382825	google-chrome-stable
       <snip>
     run this for more: dpkg-query -W -f='${Installed-Size}\t${Package}\n' | sort -n -r

     These are the top 10 largest snap packages:
       3.8G	total
       1.5G	/var/lib/snapd/snaps/kf5-core22_3.snap
       670M	/var/lib/snapd/snaps/gnome-46-2404_145.snap
       <snip>
     run this for more: sudo du -hcs /var/lib/snapd/snaps/* | sort -hr
        to remove a specific rev: "sudo snap remove pgk_name --revision=nnn"
        to retain minimal revs  : "sudo snap set system refresh.retain=2"

==== difference in usage
     initial usage   : 343.7 GB
     final usage     : 343.7 GB
     total cleaned up: 0.0

Notes:

  • your usage will be different; in my case, I saved about 50GB of disk space
  • There will be additional logging when logs, packages, snaps, etc. are deleted
  • It currently only shows the top 10 larget apt and snap packages. Change num_report = 10 in function _report_others() to see more.

- John Arrizza