Just a short post about some useful cleanup commands for Debian and Ubuntu systems. There are (to my knowledge) no build in task solving the following things
- Remove old kernels (while keeping the currently running and the latest)
- Purge removed packages (especially after autoremoving unneeded dependencies)
If this post was helpful to you, please consider supporting me.
# Remove old kernels
Debian and Ubuntu don’t remove old kernels when upgrading. Although this of course makes sense to keep the system bootable in case of a broken kernel, it can fill up /boot pretty quickly. Usually it should be sufficient to keep the currently running kernel, as well as the latest one. The rest can be safely deleted. This can be done with the following command:
A short explanation
1 2 3 4 5 6 7 8
# Purge removed packages
If you remove a package using
The packages configuration will be retained. Also, when autoremoving unneeded dependencies, apt-get by default removes packages instead of purging them.
To cleanup your system and purge all packages that are removed from the system and their dependencies, use this command
1 2 3 4 5
# Chef cookbook
Furthermore, I created the apt_cleanup Chef cookbook, which provides recipes to do all those tasks automatically.
For a convenient auto-cleanup, the following recipes can be used
Includes all other cleanup recipes
Removes all old kernels, but the most recent as well as the currenlty used one.
apt-get autoremove to remove packages not required anymore.
Purges already removed packages, to get rid of e.g. old config files.
apt-get clean to remove
.dpkg files from
# Saltstack formula
UPDATE 15th Oct. 2018: I’ve also created a salt formula to take care of cleaning up.
Either run it to clean up packages immediately:
1 2 3 4 5
1 2 3 4 5
Unlike Chef, Saltstack doesn’t run periodically. To run the cleanup scripts regularily (e.g. daily), the
apt.cleanup state installs a systemd service and timer to cleanup your system automatically on a daily basis. Feel free to use the provided apt-cleanup.service and apt-cleanup.timer files independently of Saltstack!