Skip to content
Advertisement

composer remove (uninstall) all installed packages (composer install rollback)

I have a project with the composer.lock file. I installed packages with the command:

composer install

Now I would like to rollback that composer install command to the state as it was before running it.

How to remove all packages without affecting composer.lock file? Is there any single composer command to do that?

I tried:

composer remove *

but I got:

[UnexpectedValueException]
“LICENSE” is not a valid alias.

I tried:

composer remove */*

But then I get bunch of print like:

bin/console is not required in your composer.json and has not been removed
Package “bin/console” listed for update is not locked.

Why composer remove * did not work at all? AFAIK the package name as VendorName/PackageName is a common convention for Packagist but not a must (if you use private repos) so how one would be able to remove all packages named IdontHaveAnySlash etc. at once?

I may use someting similar to:

for package in $(composer show | awk '{print $1}'); do composer remove --no-interaction --dev --no-install "$package"; done

But that is not a simple and single composer command. Also composer often complains about a package being a part (dependency) of another one so composer does not uninstall it.

Removal failed, doctrine/annotations is still present, it may be required by another package. See composer why doctrine/annotations.

As my intention is to rollback to the state that did not have any package installed but only files: composer.lock and potentially composer.json I really don’t care about any dependencies, packages versions, downloading repositories’ urls etc.

I just want to have a project without any installed dependencies as it was before.

Is there any single composer command to do that?

My:

composer --version

is:

version 2.2.7 2022-02-25 11:12:27

Following yivi answer I created a simple test to verify:

mkdir -p /tmp/composer-install
cd /tmp/composer-install

curl -o composer.json https://raw.githubusercontent.com/composer/composer/18246212db7103d0a2688febcc336f77183275ee/composer.json

curl -o composer.lock https://raw.githubusercontent.com/composer/composer/d955458f271edb4fcc055a394f90a60a8328a2a8/composer.lock

sha1sum composer.json > composer.json.sha1
sha1sum composer.lock > composer.lock.sha1

composer install

sha1sum -c composer.json.sha1
sha1sum -c composer.lock.sha1

that outputs:

composer.json: OK
composer.lock: OK

So both composer.json and composer.lock are not affected by composer install so the only one thing to achieve the rollback (uninstall) of the composer install is to remove the vendor directory

rm -rf vendor

However as yivi mentioned:

If some other plugin (e.g Symfony Flex) makes changes to your existing files during the process, you’d better have the project on top of a version control system, in which case reverting is managed by VCS, not of composer.

I did not test against that case.

Advertisement

Answer

rm -rf vendor

In any case, install should not make any changes to a lockfile, so there shouldn’t be anything to “revert” from an install but deleting the installed files.

If the lockfile does not originally exist, then it will be created.

If some other plugin (e.g Symfony Flex) makes changes to your existing files during the process, you’d better have the project on top of a version control system, in which case reverting is managed by VCS, not of composer.

As my intention is to rollback to the state that did not have any package installed but only files: composer.lock and potentially composer.json

For you to be able to run composer install at all, you need at the very least composer.json to exist. install reads from the lockfile (composer.lock), but requires the JSON configuration file to exist as well. If the lockfile does not exist, update will be run instead and the lockfile will be created.

I tried composer remove

remove is the opposite from require. It removes packages from composer.json, as require adds them. Not the opposite of install. There is no opposite of install, as it does not make much conceptual sense. If one needs to delete the installed project… one can always do so.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement