How to remove unused dependencies from composer?

PhpComposer Php

Php Problem Overview


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 ?

Php Solutions


Solution 1 - Php

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.

Solution 2 - Php

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.

Solution 3 - Php

following commands will do the same perfectly

rm -rf vendor

composer install 

Solution 4 - Php

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLorenz MeyerView Question on Stackoverflow
Solution 1 - PhpDenis PshenovView Answer on Stackoverflow
Solution 2 - PhpLorenz MeyerView Answer on Stackoverflow
Solution 3 - PhpMax WenView Answer on Stackoverflow
Solution 4 - PhpValentasView Answer on Stackoverflow