How can I remove a package from Laravel using PHP Composer?

LaravelLaravel 4PackageComposer PhpUninstallation

Laravel Problem Overview


What is the correct way to remove a package from Laravel using PHP Composer?

So far I've tried:

  1. Remove declaration from file composer.json (in the "require" section)
  2. Remove any class aliases from file app.php
  3. Remove any references to the package from my code :-)
  4. Run composer update
  5. Run composer dump-autoload

None of these options are working! What am I missing?

Laravel Solutions


Solution 1 - Laravel

Composer 1.x and 2.x

Running the following command will remove the package from vendor (or wherever you install packages), composer.json and composer.lock. Change vendor/package appropriately.

composer remove vendor/package

Obviously you'll need to remove references to that package within your app.

I'm currently running the following version of Composer:

Composer version 1.0-dev (7b13507dd4d3b93578af7d83fbf8be0ca686f4b5) 2014-12-11 21:52:29

Documentation

https://getcomposer.org/doc/03-cli.md#remove

Updates
  • 26/10/2020 - Updated answer to assert command works for v1.x and v2.x of Composer

Solution 2 - Laravel

I got it working... The steps to remove a package from Laravel are:

  1. Remove the declaration from file composer.json (in the "require" section)
  2. **Remove Service Provider from file config/app.php (reference in the "providers" array)
  3. Remove any class aliases from file config/app.php
  4. Remove any references to the package from your code :-)
  5. Run composer update vendor/package-name. This will remove the package folder from the vendor folder and will rebuild the Composer autoloading map.
  6. Manually delete the published files (read the comment by zwacky)

It will remove the package folder from the Vendor folder.

Solution 3 - Laravel

Normally composer remove used like this is enough:

composer remove vendor/package

But if a Composer package is removed and the "config" cache is not cleaned you cannot clean it. When you try like so

php artisan config:clear

you can get an error In ProviderRepository.php line 208:

> Class 'Laracasts\Flash\FlashServiceProvider' not found

This is a dead end, unless you go deleting files:

rm bootstrap/cache/config.php

And this is Laravel 5.6 I'm talking about, not some kind of very old stuff.

It happens usually on automated deployment, when you copy files of a new release on top of old cache. Even if you cleared the cache before copying. You end up with an old cache and a new composer.json file.

Solution 4 - Laravel

You can remove any package just by typing the following command in the terminal, and just remove the providers and alias you provided at the time of installing the package, if any and update the composer,

composer remove vendor/your_package_name
composer update

Solution 5 - Laravel

Before removing a package from a composer.json declaration, please remove the cache:

php artisan cache:clear  
php artisan config:clear 

If you forget to remove the cache and you get a "class not found error" then please reinstall the package, clear the cache and remove again.

Solution 6 - Laravel

You can do any one of the below two methods:

  1. Running the below command (most recommended way to remove your package without updating your other packages)

    $ composer remove vendor/package

  2. Go to your composer.json file and then run command like below it will remove your package (but it will also update your other packages)

    $ composer update

Solution 7 - Laravel

If you are still getting the error after you are done with all the steps in the previous answers, go to your projects, BootstrapCacheconfig.php. Remove the provider and aliases entries from the cached array manually.

Solution 8 - Laravel

Use:

composer remove vendor/package

This is an example:

Install or add a package
composer require firebear/importexportfree
Uninstall / remove
composer remove firebear/importexportfree

Finally after removing:

php -f bin/magento setup:upgrade

php bin/magento setup:static-content:deploy –f

php bin/magento indexer:reindex

php -f bin/magento cache:clean

Solution 9 - Laravel

In case the given answers still don't help you remove that, try this:

  • Manually delete the line in require from composer.json

  • Run composer update

Solution 10 - Laravel

To add the packages, the command is to be like:

composer require spatie/laravel-permission

To remove the packages, the command is to be like:

composer remove spatie/laravel-permission

Solution 11 - Laravel

Running

Composer remove package/name
Php artisan optimize

e.g "Composer remove mckenziearts/laravel-notify" works for me while using Laravel 8.

Solution 12 - Laravel

If this doesn't work "composer remove package/name", you can still remove it manually.
Note : package/name is like spatie etc.

  1. Go to composer.json and find the package name
  2. Delete package name from composer.json
  3. Find the vendor file in your Laravel project.
  4. Delete the package file which is under vendor
  5. run composer install on your terminal

Note : Package File mean is that package that you are looking for. For example, you want to remove Spatie. Then you need to find that with similar name in vendor file and you need to delete it manually.
Your package was removed successfully.

Solution 13 - Laravel

We have come with a great solution. This solution is practically done in Laravel 6. If you want to remove any package from your Laravel Project then you can easily remove the package by following below steps:

Step 1: You must know the package name which you want to remove. If you don't know the complete package name then you can open your project folder and go to the composer.json file and check the name in the require array:

"require": {
        "php": "^7.2",
        "fideloper/proxy": "^4.0",
        "laravel/framework": "^6.2",
        "laravel/passport": "^8.3",
        "laravel/tinker": "^2.0"
    },

Suppose, here I am going to remove the "fideloper/proxy" package.

Step 2: Open a command prompt with your project root folder directory

Enter image description here

Step 3: First of all, clear all cache by the following commands. Run the commands one by one.

php artisan cache:clear  
php artisan config:clear 

Step 4: Now write the following command to remove the package. Here you need to change your package name instead of my example package.

composer remove fideloper/proxy

Now, wait for a few seconds while your package is removed.

Solution 14 - Laravel

On Laravel 8.*, the following steps work for me:

  1. Run command composer remove package-name on the terminal

  2. Remove Provider and aliases from file Config/app.php

  3. Remove the related file from the Config folder.

Remove it from your code where you used it.

Solution 15 - Laravel

  1. Remove the package folder from the vendor folder (manual delete)
  2. Remove it from file composer.json and 'composer.lock' files (use Ctrl + F5 to search)
  3. Remove it from file config/app.php and file bootstrap/cache/config.php files
  4. Run these commands:
    composer remove **your-package-name**
    php artisan cache:clear
    php artisan config:clear
    

Solution 16 - Laravel

There are quite a few steps here:

  1. Go to file composer.json and look for the package and how it is written over there.
  • for example

> { "require": { > "twig/twig": "^3.0" } }

I wish to remove twig 3.0

  1. Now open cmd and run composer remove vendor/your_package_name as composer remove twig/twig will remove the package.

  2. As a final step, run composer update. This will surely give you a massage of nothing to install or update, but this is important in case your packages have inter-dependencies.

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
QuestionigasterView Question on Stackoverflow
Solution 1 - LaravelsteadwebView Answer on Stackoverflow
Solution 2 - LaraveligasterView Answer on Stackoverflow
Solution 3 - LaravelYevgeniy AfanasyevView Answer on Stackoverflow
Solution 4 - LaravelShahrukh AnwarView Answer on Stackoverflow
Solution 5 - LaravelRayees PkView Answer on Stackoverflow
Solution 6 - LaravelPrashant BarveView Answer on Stackoverflow
Solution 7 - LaravelRamjith ApView Answer on Stackoverflow
Solution 8 - LaravelSUNNETmediaView Answer on Stackoverflow
Solution 9 - LaravelDonJoeView Answer on Stackoverflow
Solution 10 - LaravelAman MehraView Answer on Stackoverflow
Solution 11 - LaravelVic IykeView Answer on Stackoverflow
Solution 12 - LaravelEnverView Answer on Stackoverflow
Solution 13 - LaravelSumit Kumar GuptaView Answer on Stackoverflow
Solution 14 - LaravelinfomasudView Answer on Stackoverflow
Solution 15 - LaravelAhkmy990View Answer on Stackoverflow
Solution 16 - LaravelChandraarnavView Answer on Stackoverflow