Remember These 5 apt Clean Up Utilities for Your Next Spring Cleaning

If space is getting tight on your Ubuntu system drive, try these commands to find and automatically remove unnecessary installation files. It only takes a moment, and it recovers valuable hard drive real estate.


A Primer on the apt Package Manager

There’s more than one way to skin a cat, and there’s more than one way to install software onto an Ubuntu Linux computer. You can use the apt and apt-get commands to install DEB files, or you can use Snaps or Flatpaks.


The apt (Advanced Package Tool) and apt-get commands are wrappers for the low-level dpkg command. The apt command is an updated version of apt-get. It makes things slightly simpler, and more visual, but it doesn’t replicate all the functionality of apt-get. Using apt and apt-get is the native way to install apps on Debian Linux, from which Ubuntu and its many relatives are derived.


When you install a package through apt, the DEB file is downloaded and the files stored inside it are extracted and placed where they need to be in the file system. So that it doesn’t need to be downloaded again—should you wish to reinstall or repair an installation—the DEB file is retained.

The space taken by DEB files mounts up, over time. If you’ve chosen to have a separate partition for your operating system, that partition can start to fill up because of all the stored DEB files. Even with a simple all-in-one partition set-up, you can still feel the pinch of too many archived DEB files if you have a small hard drive.

The standard way to uninstall applications with apt also leaves the application’s configuration files behind. That can be convenient. You can uninstall and reinstall an application and have the new installation find and use the settings and configuration of the previous version. But what if you want to start with a clean slate?

Thankfully, the apt family of easy-to-use tools can deal with these scenarios and more.


remove: Uninstall Apps

To uninstall a program, use the apt command and pass the remove option to it.

sudo apt remove sqlite3 

This uninstalls the application, but it leaves behind any modified configuration files, and the DEB file itself. Let’s see what’s in the /var/cache/apt/archive directory.

ls /var/cache/apt/archives/ 

As you can see, there are a bunch of DEB files that have been collected as applications have been installed, including the DEB file for SQLite3. There’s also a directory called partial, and a lock file.


You don’t need to uninstall an application before you clean out the archived DEB files. The point I was making is, even though you’ve uninstalled the application, the DEB files are left behind.

clean: Remove Installation Files

The apt clean command will remove accumulated DEB files from the /var/cache/apt/archives and /var/cache/apt/archives/partial directories.

sudo apt clean 

You’re silently returned to the command line. Let’s see what we’ve got in the archives directory now.

ls /var/cache/apt/archives/ 

It’s empty. The partial directory and the lock file are not removed (and shouldn’t be), but the partial directory is emptied.


autoclean: Remove Superseded Files

The autoclean option is counterintuitive. It does what clean does, but it only removes DEB files that can’t be downloaded anymore.

That might sound backward. Surely the ones you can’t get anymore are the ones you need to keep? It makes more sense when you think about deprecated DEB files and newer releases.

If there’s a newer version available, apt will retrieve that version if you ever reinstall that application. So the older, archived, one is redundant.

If you perform a reinstall, and there’s no newer version available, the archived version is used to perform the install, saving apt from having to download the DEB again.

purge: Uninstall Apps and Config Files

Uninstalling an application with the remove command removes files such as the application executable, its man pages, and any other supporting files such as “.desktop” files.

It doesn’t remove any user settings and configuration files that have been modified through the setup and use of the application. That means you can reinstall and have the application find and use its previous settings.


Sometimes that’s convenient, and sometimes it’s an issue. If there’s a bad setting in the configuration files preventing the application from behaving normally. Uninstalling and reinstalling the application won’t change anything, because the old configuration files will be re-used.

The answer is to purge the application, removing the application and all associated files.

sudo apt purge sqlite3 

One interesting point to note, you can use apt purge on applications that have already been uninstalled.

autoremove: Remove Orphaned Dependencies

Removing applications, even purging them, can leave behind libraries and other files that were installed as dependencies of the application you’ve just uninstalled.


Suppose application A needs library Y. The apt command detects that dependency, and installs library Y while application A is installed. If you then install application B, and it requires library Y, it’ll use the already installed version of library Y.

The autoremove option uninstalls dependencies that are no longer required.

Some care is required here. I’ve run across instances where autoremove removes a library because application A is no longer on the computer, but application B still needs it. This leaves application B inoperable. Reinstalling application B usually cures this type of situation.

sudo apt autoremove 

You’re shown the files that will be removed, and you need to confirm you wish to delete them.

Don’t Hoard DEBs for No Reason

Cleaning out the residue from applications that have been removed from your computer, can free up hard drive space for you. It’s a good habit to get into, as part of your periodic system maintenance chores.


As usual, you can refer to the man pages to read more about these commands, but be aware that some options for apt, such as clean and autoclean, are described in the man pages for apt-get, not the man pages for apt.


Source link
Exit mobile version