remove composer package from laravel application

Composer is an application-level package manager for the PHP programming language that provides a standard format for managing dependencies of PHP software and required libraries.

As a PHP developer, you will need to add or remove some functionality daily. These Composer packages can be anything that saves you time and provides you with proper functionality. Packages like Carbon can help you with dates Spatie Media Library can help you with managing media in your application or Debugbar can handle debugging purposes.

Sometimes you add a package for your function requirement but later on, you need to remove those functionalities. For example, you need to create a sitemap for your application. so you use the spatie/laravel-sitemap package to create a sitemap. But later on, you wish to use some third-party sitemap generator. So you need to remove this package.

Adding a package to Laravel

First of we need to add a package to our Laravel application. We will remove this package in the next step.

As per example, We are going to use the Laravel Sitemap package which is handy to create a sitemap programmatically for large sites. To install this package enter the below command into the terminal :

composer require spatie/laravel-sitemap

It will take some time to download/install the package. But when it completes, It will automatically add the below line to your composer.json file.

It will also add other files to the vendor/spatie/laravel-sitemap directory. Those files are core files for a package.

"spatie/laravel-sitemap": "^5.8"

Remove or Uninstall Composer Package

With below command, we have installed the sitemap generator package to our application now we can remove it using the below command :

composer remove spatie/laravel-sitemap

This will revert changes to the compsoer.json file and as well as remove a directory from the vendor folder.

Now your package is removed from the application. Don’t forget that you have to edit your code and ensure that the package is not being used.

Generally, you need to remove a package from the config/app.php file as providers and aliases. If you can those methods statically then you need to check it in the related functionality of your code.