pkg-clean: Remove obsolete packages

Hi
I want to remove obsolete/duplicate packages from (/var/cache/pkg). There are different versions of same package present in that directory. For instance, youtube-dl has three different versions in cache directory:
Code:
youtube_dl-2020.12.07.txz
youtube_dl-2020.12.12.txz
youtube_dl-2020.12.14.txz
So, I want to delete all of them except the one which is installed in my system. I tried to use "pkg clean", but it is removing the binaries of all the packages (whether installed or not).
The man page of "pkg clean" says to use "pkg clean -a" to remove everything, but "pkg clean" is already removing everything without "-a" option. So, kindly guide me that how can I remove only those packages from (/var/cache/pkg) which are not installed in my system?
 
pkg autoremove -y
portmaster --clean-distfiles -y

The portmaster command is a bonus in case you use ports... Pkg autoremove should be fine.
 
pkg autoremove -y
portmaster --clean-distfiles -y

The portmaster command is a bonus in case you use ports... Pkg autoremove should be fine.
Doesn't "pkg autoremove" removes/uninstall obsolete/orphan (installed) packages?
I am looking for removing obsolete binaries from cache (var/cache/pkg).
 
I want to remove obsolete/duplicate packages from (/var/cache/pkg). There are different versions of same package present in that directory. For instance, youtube-dl has three different versions in cache directory:
Do you really need to keep the latest version in /var/cache/pkg ?
I've never needed to make use of any of these files and usually delete them all after a successful run of pkg upgrade.
 
The only usecase would be the need to reinstall a package combined with a slow connection to your next pkg mirror. I was also wondering whether this is really relevant to someone…
 
Do you really need to keep the latest version in /var/cache/pkg ?
I've never needed to make use of any of these files and usually delete them all after a successful run of pkg upgrade.

The only usecase would be the need to reinstall a package combined with a slow connection to your next pkg mirror. I was also wondering whether this is really relevant to someone…
It is better to keep the (installed) version in cache on my metered connection. So, that if I have to reinstall anything, I may not have to download it again.
 
This is my six steps routine to update/clean packages:

1. pkg update -f
2. pkg upgrade
3. pkg autoremove
4. pkg audit -F
5. pkg clean
6. pkg stats
 
Doesn't solve the problem as, indeed, pkg clean also removes currently installed AND available packages from the cache.

Maybe this is a bug in pkg. I never cared cause re-downloading a package for a reinstall isn't a problem for me…
 
It does indeed seem to delete everything, hadn't noticed it before because I always use pkg clean -a (have a local repository, I have no need for the cache). So yes, this looks like a bug.
 
zetrotrack000 : +1 for bug report.

If it's possible edit your last post and put the bug number in the PR tag, i.e. [PR]Problem Report Number[/PR]
For example in you case: [pr]252055[/pr].
Using PR tag makes it easier to follow up on the related reply/content in the Forums. Thanks.
 
zetrotrack000 : +1 for bug report.

If it's possible edit your last post and put the bug number in the PR tag, i.e. [PR]Problem Report Number[/PR]
For example in you case: [pr]252055[/pr].
Using PR tag makes it easier to follow up on the related reply/content in the Forums. Thanks.
I have amended the changes in my above post. Thanks for guiding me towards proper method :)
 
  • Thanks
Reactions: a6h
I use a bash script to update my system:
Bash:
#!/usr/bin/env bash

if [[ "${UID}" != "0" ]] ; then echo "UID != 0" ; exit ; fi

printexec() {

    printf "\e[91;1m * ${*}\e[0m\n"
    "${@}"

}

printexec freebsd-update fetch install
printexec pkg update
printexec pkg upgrade
printexec pkg autoremove
printexec pkg audit -F -r
printexec pkg clean
printexec portsnap fetch update
printexec portmaster --clean-distfiles
printexec pkg stats

printf "\e[91;1m * Done\e[0m\n"

exit 0
 
I needed to clear a jail's cache of stale binaries because of its ZFS quota, so I wrote this:

Bash:
#!/usr/local/bin/bash
# clear cache of superseded packages and all unlinked files

find ${1:?"error: target directory required"} ! -type d | sort -V |
awk -v dir="$1" -v readlink="/usr/bin/readlink" -v rm="/bin/rm" '
BEGIN { dir = dir ~ "/$" ? dir : dir "/" } # ensure dirname has trailing /
      { sub(/\/.+\//, "")                } # strip pathname from filename
/[-~][[:alnum:]]{10}.(txz|pkg)$/ {         # package files end in 10-alnum chars
        pkgs[$0]                           # store names of all packages
        next                             } # proceed to next item in dir
system("[ ! -e " dir $0 " ]") {            # only unbroken links are assessed
        link = $0
        sub(/-[^-]+$/, "", link)           # strip version numbers and extension
        newest[link] = $0                } # keeps last link-of-type: the newest
      { links[$0]                        } # store names of all links
END   { for (link in newest) {             # remove newest packages from arrays
            readlink " " dir newest[link] | getline target # get symlink target
            delete links[newest[link]]; delete pkgs[target]
            close(readlink " " dir newest[link]) }
        for (link in links) system(rm " " dir link) # delete all remaining links
        for (pkg in pkgs) system(rm " " dir pkg)    #  and all unlinked files
      }'

exit

Warning: it deletes binaries that aren't linked, and only keeps the most recent package that isn't a broken link (which might not be the most recent version). It's what I wanted, but might not be what others are looking for.

Example:
foo.1.txz -> foo.1~blah.txz
foo.2.txz -> foo.2~blah.txz
If foo.2~blah.txz doesn't exist, foo.2.txz is deleted, but foo.1.txz and foo.1~blah.txz are kept.
If both links are broken, neither is kept.
If both links are good, foo.2.txz and foo.2~blah.txz are kept.
If foo.3~blah.txz exists but is not linked to, it's deleted.

Probably a better approach would be to create a new link to foo.3~blah.txz and delete the other links and binaries, but mine was a disk constraint, not network.
 
Back
Top