I was getting a bit cramped for space on the root partition of an older desktop, which has been running for 4 years or so, and discovered that although I'd been running the 6.x kernel for several years, there was a lot of baggage from 5.x kernels lying around. Here's a reminder to myself how to clear it up. If you've done an inplace upgrade from Ubuntu 22 to 24, you're probably in the same position.
So first of all, check your running kernel. You definitely want to keep that one! Then check what kernels you've got installed
# Current kernel
uname -r
6.8.0-101-generic
# What kernels do we have installed.
# ii at the beginning of the line means installed, but rc entries might have some residual config files.
dpkg --list | grep 'linux-image-'
Now, if you're certain that you want to get rid of all the kernels beginning with 5.x, then go ahead and run the following. Or otherwise edit the commands so that you're disposing of the ones you want to get.
sudo apt remove $(dpkg --list | grep 'linux-image-5\.' | awk '{print $2}' | xargs)
# And now we get rid of all the associated modules and headers
sudo apt remove $(dpkg --list | grep 'linux-headers-5\.' | awk '{print $2}' | xargs)
sudo apt remove $(dpkg --list | grep 'linux-modules-5\.' | awk '{print $2}' | xargs)
sudo apt remove $(dpkg --list | grep 'linux-modules-extra-5\.' | awk '{print $2}' | xargs)
# And clean up
sudo apt autoremove --purge
sudo update-grub
So that cleared over a Gigabyte. Ubuntu will generally (I think) keep the last 3 kernels, but sometimes it doesn't seem to clean up everything it can.