# pkg-clean: Remove obsolete packages



## usakhncit (Dec 22, 2020)

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:

```
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?


----------



## Lamia (Dec 22, 2020)

pkg autoremove -y
portmaster --clean-distfiles -y 

The portmaster command is a bonus in case you use ports... Pkg autoremove should be fine.


----------



## usakhncit (Dec 22, 2020)

Lamia said:


> 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).


----------



## Alexander88207 (Dec 22, 2020)

zetrotrack000 said:


> I am looking for removing obsolete binaries from cache (var/cache/pkg).



`pkg clean -a`


----------



## rawthey (Dec 22, 2020)

zetrotrack000 said:


> 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`.


----------



## zirias@ (Dec 22, 2020)

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…


----------



## usakhncit (Dec 22, 2020)

Alexander88207 said:


> `pkg clean -a`


It removes everything from cache directory.


----------



## usakhncit (Dec 22, 2020)

rawthey said:


> 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`.





Zirias said:


> 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.


----------



## a6h (Dec 22, 2020)

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`


----------



## zirias@ (Dec 22, 2020)

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…


----------



## SirDice (Dec 22, 2020)

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.


----------



## usakhncit (Dec 22, 2020)

I have filed a bug which can be tracked at PR 252055


----------



## a6h (Dec 22, 2020)

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.


----------



## usakhncit (Dec 23, 2020)

vigole said:


> 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


----------



## BobSlacker (Dec 27, 2022)

I use a bash script to update my system:

```
#!/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
```


----------

