Spread the love

Composer is used to manage the dependencies of project and sometimes we wanted to remove installed packages from laravel. As we can add the package using simple command composer install package_name in the same way we can remove the package using

composer remove package_name

In this article i will show you to remove the the package completely from the project. To remove the package we can use above command if we have not used this package classes in anywhere in our project but what if we have used the classes of this project in our project like config\app.php and also the service provider. In some cases we manually add service provider and alias so we also need to remove them manually.

If you just want to remove the package from vendor then you can use above command composer remove package_name or if your package is not auto discovery featured off then you can use below steps to remove the installed package completely.

How to remove installed package completely manually in laravel ?

In some cases above command not work so we can follow the below steps to remove the package completely from project. we will take an example of https://github.com/tylernathanreed/laravel-relation-joins package so lets start remove installed package in laravel step by step

Step 1 : Remove declaration from composer.json

Firstly we need to open compose.json file and remove the "reedware/laravel-relation-joins": "^2.4" from the file

 "require": {
        "php": "^7.3|^8.0",
        "doctrine/dbal": "^3.3",
        "fruitcake/laravel-cors": "^2.0",
        "guzzlehttp/guzzle": "^7.0.1",
        "harimayco/laravel-menu": "^1.4",
        "laravel/framework": "^8.54",
        "laravel/sanctum": "^2.11",
        "laravel/tinker": "^2.5",
        "readerstacks/querymigration": "^1.1",
        "reedware/laravel-relation-joins": "^2.4"
    },

Updated file:

 "require": {
        "php": "^7.3|^8.0",
        "doctrine/dbal": "^3.3",
        "fruitcake/laravel-cors": "^2.0",
        "guzzlehttp/guzzle": "^7.0.1",
        "harimayco/laravel-menu": "^1.4",
        "laravel/framework": "^8.54",
        "laravel/sanctum": "^2.11",
        "laravel/tinker": "^2.5",
        "readerstacks/querymigration": "^1.1",
    },

Step 2 : Remove service provider of package

While we add our package in laravel we also add service provider so we also need to remove it from config\app.php file if the package not support auto discovert feature. After open the file go to providers array and remove the entry as below

 'providers' => [

        /*
         * Laravel Framework Service Providers...
         */
        ....
        Reedware\LaravelRelationJoins\LaravelRelationJoinServiceProvider::class,
        Readerstacks\QueryMigration\QueryMigrationServiceProvider::class,
  ],

Updated File :

 'providers' => [

        /*
         * Laravel Framework Service Providers...
         */
        ....
        Readerstacks\QueryMigration\QueryMigrationServiceProvider::class,
  ],

Step 3 : Remove alias of package

In some packages we also add our package in laravel alias list so we also need to remove it from config\app.php file. After open the file go to aliases array and remove the entry as below

 'aliases' => [

        /*
         * Laravel Framework Class Aliases...
         */
        ....
         'QrCode' => SimpleSoftwareIO\QrCode\Facades\QrCode::class,
         'Date' => App\Facades\DateHelperFacade::class,
  ],

If you don’t find any alias then you can skip this step.

Step 4 : Remove references of package from project code

May be we have used the some code and classes of this package in our code so we need to remove it . For example i have used the the code of this package in my controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use LaravelRelation;  //Need to remove


class ArticleController extends Controller
{

    public function index(Request $request)
    {
        User::query()->joinRelation('posts'); // i need to remove this
    }
}

Updated File :

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ArticleController extends Controller
{

    public function index(Request $request)
    {
         
    }
}

Step 5: Run composer command

Now run composer update or delete command

composer update vendor/package-name
//or 
composer update
composer dump-autoload
Note : You also need to remove assets and views file of package if you have published while installing the package

Step 6: Clear Cache

Also clear the cache of routes, config and view so that we will have not any cache in app


php artisan cache:clear
php artisan config:clear
php artisan route:clear 
php artisan view:clear

Leave a Reply