273

I installed a package with composer, and it installed many other packages as dependencies.

Now I uninstalled the main package with composer remove packageauthor/packagename, but all the old dependencies were not removed. I expected composer to clean up and only keep packages that are required according to composer.json and their dependencies.

How can I force composer to clean up and remove all unused packages ?

Machavity
  • 30,841
  • 27
  • 92
  • 100
Lorenz Meyer
  • 19,166
  • 22
  • 75
  • 121

4 Answers4

468

The right way to do this is:

composer remove jenssegers/mongodb --update-with-dependencies

I must admit the flag here is not quite obvious as to what it will do.

Update

composer remove jenssegers/mongodb

As of v1.0.0-beta2 --update-with-dependencies is the default and is no longer required.

user151841
  • 17,377
  • 29
  • 109
  • 171
Denis Pshenov
  • 11,157
  • 6
  • 42
  • 42
  • thanks to @Yehosef for starting the issue on github where a solution was provided, but since he did not update his answer I decided to write my own for everyones convenience – Denis Pshenov Apr 03 '15 at 18:52
  • 3
    One thing to note: If you've already `composer remove …`'d, but forgot the `--update-with-dependencies` until you stumble across this question — subsequent calls to `composer remove` won't kill all dependencies. You'll need to revert, composer `install`, then `composer remove --update-with-dependencies ` – Mike G Sep 15 '15 at 06:23
  • Now I get the following message: `You are using the deprecated option "update-with-dependencies". This is now default behaviour. The --no-update-with-dependencies option can be used to remove a package without its dependencies` – Pathros Apr 08 '16 at 18:00
  • 4
    **How about cleanup after the removal of a package?** -- That's where @LorenzMeyer answer goes in: ``composer update`` Thumbs up to this answer to @LorenzMeyer – Allen Linatoc Jun 07 '16 at 13:49
  • This is not the answer to the question. This answers "how to remove a package". `composer update` also doesn't work perfectly because it will upgrade some packages on the way. `rm -rf vendor && composer install` is the valid answer. – jtompl Oct 19 '20 at 17:07
37

In fact, it is very easy.

composer update

will do all this for you, but it will also update the other packages.

To remove a package without updating the others, specifiy that package in the command, for instance:

composer update monolog/monolog

will remove the monolog/monolog package.

Nevertheless, there may remain some empty folders or files that cannot be removed automatically, and that have to be removed manually.

Lorenz Meyer
  • 19,166
  • 22
  • 75
  • 121
  • 9
    the unfortunate part about this is that it forces you to update your packages instead of just removing old versions. Eg, If I have two packages AAA/aaa ~1 and version BBB/bbb ~2 and I am currently at version 1.0 of AAA/aaa and they released 1.1, then when I delete BBB/bbb I am automatically updated (which may not be what I was intending to do..) – Yehosef Feb 16 '15 at 15:05
  • 1
    You are right. If you have a better solution, come back and post it as an answer. – Lorenz Meyer Feb 16 '15 at 15:23
  • 3
    I don't yet - I just want to point out for others a pitfall of this approach. – Yehosef Feb 16 '15 at 16:33
  • it really should be an flag on the composer install .. something like `--prune` – Yehosef Feb 16 '15 at 16:36
  • 1
    if you like the idea you can vote on it - https://github.com/composer/composer/issues/3751 – Yehosef Feb 16 '15 at 16:46
  • @yehosef it looks like you got the correct answer over there. Did you? – Lorenz Meyer Feb 17 '15 at 07:35
  • I replied - I don't think their suggesting is viable. – Yehosef Feb 17 '15 at 09:15
  • Is it possible to tell `composer update` to keep unused dependencies inside the vendor directory? I do not want them to be removed, even if they are not referenced in `composer.json` anymore... – tonix Mar 08 '18 at 09:34
  • @Yehosef you're doing `update` basically asking for the updates, you can always do `composer install` and it will delete the non used packages without updating – Yidir Jun 11 '19 at 00:44
34

following commands will do the same perfectly

rm -rf vendor

composer install 
Jonas Staudenmeir
  • 24,815
  • 6
  • 63
  • 109
Max Wen
  • 897
  • 8
  • 6
15

Just run composer install - it will make your vendor directory reflect dependencies in composer.lock file.

In other words - it will delete any vendor which is missing in composer.lock.

Please update the composer itself before running this.

Valentas
  • 2,135
  • 1
  • 17
  • 21