88

I am using Perl running in user space (not installed via root) and installing modules via the command-line cpan. I would like to know if there is a simple way to remove a module without having to do a lot of work deleting individual files.

I searched for this question on the internet and found some answers, but the answers I've found seem to either discuss using the Perl package manager (specific for Microsoft Windows), otherwise operating-system specific (BSDpan), suggesting using cpanplus (which I've had several bad experiences with), or ended by pointing to a dead link as follows: http://www.cpan.org/misc/cpan-faq.html#How_delete_Perl_modules.

My question is specifically whether there is a clean way to remove a module installed via cpan.

brian d foy
  • 129,424
  • 31
  • 207
  • 592
  • 37
    You really can't think of any reason to want to remove any Perl module from any system? –  Apr 13 '10 at 12:11
  • 1
    To manage packages, use a package manager -- which CPAN is not. – jrockway Apr 13 '10 at 13:14
  • 5
    I for one, need to get rid of a few modules so I will have space on my 2GB Raspberry Pi – Ray Feb 22 '13 at 19:15
  • 8
    @AmbroseChapel: an obvious reason is to test code that detects whether end users have the required Perl module installed during start up. Which is where I'm at now. I guess I have to run up a clean VM to run that test instead. – AlwaysLearning Oct 17 '14 at 23:19
  • 5
    @AmbroseChapel "Why" is not necessary to answer a technical question. – felwithe Sep 08 '15 at 02:28
  • Use cpanp (cpanplus) as [mentioned here](https://stackoverflow.com/questions/7777252/uninstall-all-perl-modules-installed-by-cpan) – FearlessHyena Mar 29 '18 at 06:48
  • 3
    And another reason (even though the original comment seems to be deleted) - you installed a module via `cpan`, then found out it's available in the distribution packages. – Richlv Sep 13 '18 at 09:47

6 Answers6

75
  1. Install App::cpanminus from CPAN (use: cpan App::cpanminus for this).
  2. Type cpanm --uninstall Module::Name (note the "m") to uninstall the module with cpanminus.

This should work.

G. Cito
  • 6,210
  • 3
  • 29
  • 42
Aedla
  • 751
  • 5
  • 2
  • It seems that installing the `App::cpanminus` module with `cpan App::cpanminus` doesn't install any `cpanm` tool, if I type `cpanm --uninstall Module::Name` I get `-bash: cpanm: command not found` on OS X. Am I missing something – tonix Sep 13 '15 at 19:28
  • @tonix: Check if its location is in your `$PATH`. – reinierpost Feb 27 '17 at 13:30
  • It was an old comment. Do you mean using `find . -name '*cpanm' | grep -E 'cpanm'` ? – tonix Feb 28 '17 at 08:34
  • on a mac, you do not need to use find. It is much faster to use spotlight `mdfind cpanm` – Armand Oct 18 '17 at 23:10
  • 4
    Using Perl v5.22.1 I did `sudo cpan install "DateTime::Format::Builder"`, everything OK, then `cpanm --uninstall "DateTime::Format::Builder"` and I get _is not found in the following directories and can't be uninstalled_. Those are: `/usr/local/lib/x86_64-linux-gnu/perl/5.22.1` and `/usr/local/share/perl/5.22.1`. I installed `cpanm` with `sudo apt install cpanminus` (v1.7040). Any Idea? – Pablo Bianchi Apr 06 '18 at 04:10
  • This only uninstalls libs, not binaries from `/usr/bin` :/ – xeruf Sep 17 '21 at 08:38
49

You can't. There isn't a feature in my CPAN client to do such a thing. We were talking about how we might do something like that at this weekend's Perl QA Workshop, but it's generally hard for all the reasons that Ether mentioned.

brian d foy
  • 129,424
  • 31
  • 207
  • 592
  • Python had the same problem with `setuptools`. It was solved in `pip` by using (or generating upon installation) package metadata that includes the list of files. – ivan_pozdeev Aug 18 '15 at 15:36
  • Python doesn't really solve that problem. People have been talking about doing the same thing in Perl, but it won't work reliably because modules may not make their metadata available. – brian d foy Aug 18 '15 at 20:27
  • 2023: has this situation not changed in the past 13 years? Is there a more recent source for info? Can you add an ___ADDENDUM (2023):___ to this post? – jubilatious1 Apr 22 '23 at 09:08
  • Things have not changed. The answer is as good now as it was then. – brian d foy Apr 26 '23 at 03:28
32

As a general rule, there is not a specific 'uninstall' mechanism that comes with CPAN modules. But you might try make uninstall in the original directory the module unpacked into (this is often under /root/.cpan or ~/.cpan), as some packages do contain this directive in their install script. (However, since you've installed modules into a local (non-root) library directory, you also have the option of blowing away this entire directory and reinstalling everything else that you want to keep.)

A lot of the time you can simply get away with removing the A/B.pm file (for the A::B module) from your perllib -- that will at least render the module unusable. Most modules also contain a list of files to be installed (called a "manifest"), so if you can find that, you'll know which files you can delete.

However, none of these approaches will address any modules that were installed as dependencies. There's no good (automated) way of knowing if something else is dependent on that module, so you'll have to uninstall it manually as well once you're sure.

The difficulty in uninstalling modules is one reason why many Perl developers are moving towards using a revision control system to keep track of installations -- e.g. see the article by brian d foy as a supplement to his upcoming book that discusses using git for package management.

Henk Langeveld
  • 8,088
  • 1
  • 43
  • 57
Ether
  • 53,118
  • 13
  • 86
  • 159
  • 3
    That's not an excerpt from the book. It's extra stuff you won't find in Effective Perl Programming. – brian d foy Apr 13 '10 at 06:49
  • 1
    "There's no good (automated) way of knowing if something else is dependent on that module" - why isn't there a way of knowing? –  Apr 13 '10 at 07:32
  • 1
    @brian: ah sorry, I misunderstood the nature of that site then! – Ether Apr 13 '10 at 16:14
25

There are scripts on CPAN which attempt to uninstall modules:

ExtUtils::Packlist shows sample module removing code, modrm.

daxim
  • 39,270
  • 4
  • 65
  • 132
bsb
  • 1,847
  • 26
  • 24
  • 2
    I guess the real question is: is there anything wrong with just deleting it from your Perl directory? – felwithe Sep 08 '15 at 02:30
19

Update 2013: This code is obsolescent. Upvote bsb's late-coming answer instead.


I don't need to uninstall modules often, but the .packlist file based approach has never failed me so far.

use 5.010;
use ExtUtils::Installed qw();
use ExtUtils::Packlist qw();

die "Usage: $0 Module::Name Module::Name\n" unless @ARGV;

for my $mod (@ARGV) {
    my $inst = ExtUtils::Installed->new;

    foreach my $item (sort($inst->files($mod))) {
        say "removing $item";
        unlink $item or warn "could not remove $item: $!\n";
    }

    my $packfile = $inst->packlist($mod)->packlist_file;
    print "removing $packfile\n";
    unlink $packfile or warn "could not remove $packfile: $!\n";
}
Community
  • 1
  • 1
daxim
  • 39,270
  • 4
  • 65
  • 132
2

Since at the time of installing of any module it mainly put corresponding .pm files in respective directories. So if you want to remove module only for some testing purpose or temporarily best is to find the path where module is stored using perldoc -l <MODULE> and then simply move the module from there to some other location. This approach can also be tried as a more permanent solution but i am not aware of any negative consequences as i do it mainly for testing.

shivams
  • 2,597
  • 6
  • 25
  • 47