# Delete packages recursively

15 July 2012 | Tags: haskell, bash

Maintaining GHC’s package database in consistent state requires some effort. There are tools like cabal-dev which allows to use per-project package database, but it convenient to use global database when you write small throwaway programs.

In fact it’s not very difficult to avoid cabal hell if you don’t have too many dependencies. Recipe is to avoid having two different version of the same package at same time. So if new version of package is installed old version should be deleted or rather unregistered.

However ghc-pkg will refuse to unregister packages if other packages depend on it so they are have to removed too. In the end I wrote simple bash script which removes package and all packages which depends on it:

ghc-pkg-force-remove() {
ghc-pkg unregister "$1" if [$? != 0 ]; then
# Check that package is indeed here
if ghc-pkg unregister "$1" 2>&1 | grep -E '^ghc-pkg: cannot find package' > /dev/null; then return fi # Happily remove everything for i in$( ghc-pkg unregister "$1" 2>&1 | sed -e 's/.*would break the following packages://; s/(.*//'); do echo ' * Removing' "$i"
ghc-pkg unregister "$i" done ghc-pkg unregister "$1"
fi
}