Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does conda know which packages are unused?

Tags:

python

conda

conda clean --packages removes unused packages from writable package caches. What is this 'writable package cache', and how is conda able to detect that it's unused?
Is it actually running through all of the python files and looking for dependencies? Or does it keep a record of what has run before?
Does it ever remove packages that I installed via pip but never used?

like image 710
Jaden Lorenc Avatar asked Oct 19 '25 05:10

Jaden Lorenc


1 Answers

Conda counts hardlinks

Conda uses hardlinks to minimize physical disk usage. That is, a single physical copy of lib/libz.a may be referenced from the package cache (where it was first unpacked), and then in multiple environments.

Conda determines eligibility for removing a package from the package cache by counting the number of hardlinks for the files in each package. Hardlink counts are tracked by the filesystem, not by Conda. An outline of the relevant code is:

# keep a list of packages to remove
pkgs_to_remove = []

# look in all package caches (there can be multiple)
for pkg_cache in pkgs_dirs:

  # check all packages
  for pkg in pkg_cache:

    # assume removable unless...
    remove_pkg = True
    for file in pkg:
       # is there evidence that it is linked elsewhere?
       if num_links(file) > 1:
         # if so, don't remove, and move on
         remove_pkg = False
         break

    # add it to list is removable
    if remove_pkg:
      pkgs_to_remove.append(pkg)

# output some info on `pkgs_to_remove`
# check if user wants to execute removal

That is, if any file in a package has more than one link, then Conda will conclude it is used in another environment, and move on to the next package.

Note that filesystems don't keep track of symbolic links (a.k.a., symlinks, softlinks), and Conda doesn't track them, hence, Conda warns about cleaning packages in combination with the allow_softlinks setting.

like image 187
merv Avatar answered Oct 21 '25 19:10

merv