Composer: Remove a Package, Clean Up Dependencies, Don't Update Other Packages
You are managing a project where squizlabs/php_codesniffer is locked to version 2.0.0, while phpmd/phpmd sits at 2.1.3. The latter brings along four transitive dependencies: pdepend/pdepend, and three Symfony components (config, dependency-injection, filesystem). All of these are recorded in your composer.lock file and present in your vendor directory.
A new version of PHP_CodeSniffer, 2.1.0, has been released, but you have no intention of upgrading yet. You simply want to drop PHPMD from the project entirelyâremoving it from the manifest, clearing its binaries from the vendor folder, and stripping out its associated Symfony dependenciesâwithout triggering an upgrade for the static analysis tool.
Running php composer.phar remove phpmd/phpmd gets you halfway there. It updates composer.json to drop the package specification, regenerates composer.lock without PHPMD, and cleans up the vendor directory. However, it leaves the four Symfony packages in both vendor and composer.lock, because they are no longer marked as required by any top-level dependency. Composer assumes these orphaned libraries should remain installed unless explicitly told otherwise.
Alternatively, if you follow up with php composer.phar update, the lock file is regenerated correctly and the orphaned dependencies vanish from your vendor folder. But this command also evaluates every version constraint in composer.json. Since PHP_CodeSniffer now has a newer patch available, Composer bumps it to 2.1.0, violating your requirement to stay on the current release.
The core issue lies in how Composer treats transitive dependencies after a package is removed. When you remove a parent package, its children become orphaned. Composer does not automatically purge them because they might still be referenced elsewhere (though in this case, they are not). You need a way to tell Composer: "Remove this package and everything that depends on it, but keep the rest of the tree exactly as is."
The Solution: Remove, Then Update Only What Is Needed
To achieve your goals without touching the version constraint of PHP_CodeSniffer, you must explicitly remove the orphaned dependencies from the lock file after removing the parent package.
Run the removal command first:
php composer.phar remove phpmd/phpmd
This updates composer.json and regenerates composer.lock, leaving the four Symfony packages in place but unlinked to any requirement. Next, manually delete those packages from the vendor folder:
rm -rf vendor/pdepend/pdepend
rm -rf vendor/symfony/config/
rm -rf vendor/symfony/dependency-injection/
rm -rf vendor/symfony/filesystem/
At this point, your vendor directory is clean of PHPMD and its children, and your composer.json no longer lists PHPMD. The lock file still contains the orphaned entries, which will cause a diff warning if you try to commit or share the project, but they will not break your application since they are unused.
To fully clean the lock file without upgrading PHP_CodeSniffer, you must regenerate it in a way that respects existing constraints. Run:
php composer.phar update --no-dev
Waitâthis won't work as expected because update will still evaluate all version constraints and may bump PHP_CodeSniffer if it thinks it's safe. Instead, use the following approach: remove the orphaned packages from the lock file manually or use a tool designed for this exact scenario.
However, Composer itself does not provide a built-in flag to "remove only these dependencies without updating others." The most reliable method is to regenerate the lock file by running an update that explicitly ignores version bumps for packages you don't want changed. Unfortunately, Composer lacks a direct --ignore-updates flag for specific packages in older versions.
A safer workaround is to temporarily relax the constraint on PHP_CodeSniffer, run the update to clean the lock file, then tighten it back:
php composer.phar require 'squizlabs/php_codesniffer:*' --with-dependencies
php composer.phar remove phpmd/phpmd
php composer.phar update --no-dev
php composer.phar require 'squizlabs/php_codesniffer:~2.0' --with-dependencies
This sequence forces Composer to re-evaluate the entire dependency tree, removing orphaned packages and respecting your original constraint on PHP_CodeSniffer in the final step. Note that this temporarily allows * (any version) for PHP_CodeSniffer during the update phase; ensure you are comfortable with this temporary state if others will review your repository mid-process.
Alternatively, you can manually edit composer.lock to remove the orphaned entries after the initial removal, then run a dry-run update to verify no other packages were affected:
php composer.phar dump-lock --no-update
This command regenerates the lock file based on current requirements without installing anything, ensuring consistency without triggering unwanted upgrades. If your version of Composer supports it, use composer update --with-all-dependencies selectively or script a cleanup routine that targets only the specified orphaned packages.
In summary, removing a package and its dependencies cleanly in Composer requires careful sequencing. The default remove command leaves orphaned transitive dependencies behind. Manually pruning those from the vendor folder and then regenerating the lock file with an update commandâwhile temporarily adjusting constraints if neededâensures your project remains stable and unchanged for all other packages. This approach respects your versioning strategy and keeps your dependency graph tidy without accidental upgrades.
