[php] How to remove unused dependencies from composer?

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 ?

This question is related to php composer-php

The answer is


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.


following commands will do the same perfectly

rm -rf vendor

composer install 

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.