Search | Sailfish OS | Running | PineTime | All Posts

Regain space on FreeBSD by removing obsolete packages/ports

January 09, 2023 — Nico Cartron

A quick one explaining how I easily gained disk space on one of my FreeBSD servers.


The context

On one of my VM (running FreeBSD 13.1-RELEASE), which I used mostly as web server, I got from time to time an issue with disk space.

Most of the time, I quickly fixed the problem by removing some backups (since they are scp'ed into another machine), but I wanted to fix the problem for real.

Disk

Here's the initial disk space I had:

root@freebsd:~ # df -h
Filesystem Size Used Avail Capacity Mounted on
/dev/vtbd0p2 46G 29G 13G 70% /

13 GB free, while OK, is not much especially since I do weekly backups.

Let's gain some space

Since I used both ports and packages on that machine, I decided to do some cleanup.

Step 1: pkg autoremove

According to the pkg man page, pkg autoremove "deletes packages which were automatically installed as dependencies and are not required any more."

Seems like a good first candidate, let's do it!

pkg autoremove -y

Number of packages to be removed: 223
The operation will free 912 MiB.

Once done, we have:

root@freebsd:~ # df -h
Filesystem Size Used Avail Capacity Mounted on
/dev/vtbd0p2 46G 28G 14G 67% /

Roughly 1 GB save - not much, let's continue!

Step 2: portmaster

How about cleaning the ports?

portmaster has an option just for that: --clean-distiles:

"recurse through the installed ports to get a list of distinfo files, then recurse through all files in /usr/ports/distfiles to make sure that they are still associated with an installed port. If not, offer to delete the stale file. With the -t option a distfile is considered valid if it is in use by any port, not just those installed.

-y --clean-distfiles does the same as above, but deletes all files without prompting."

Brilliant, let's try!

portmaster --clean-distfiles -y

===>>> Gathering distinfo list for installed ports
===>>> Checking for stale distfiles

Which gives:

root@freebsd:~ # df -h
Filesystem Size Used Avail Capacity Mounted on
/dev/vtbd0p2 46G 20G 22G 48% /

=> 8 GB regained, not too bad!

Step 3: pkg clean

Let's try a last option: pkg clean "cleans the local cache of fetched remote packages."

pkg clean -a

gives us:

root@freebsd:~ # df -h
Filesystem Size Used Avail Capacity Mounted on
/dev/vtbd0p2 46G 20G 22G 47% /

It saved a bit of disk, even though it's not obvious with df -h, a df -k would have showed it: the disk went from 48% capacity used to 47%.

Wrap Up

A quick and easy way to gain space on a FreeBSD machine - I'll add this to my maintenance routine :)

Are you using other tools? Let me know on Twitter!


Tags: FreeBSD, English