Overview
To remove a package from Laravel using PHP Composer, follow these steps:
1. Uninstall the Package Using Composer
Run the following command in your terminal:
composer remove vendor/package-name
Note: Replace vendor/package-name
with the name of the package you want to remove. For example, to remove a package called spatie/laravel-permission
,
use:
composer remove spatie/laravel-permission
This will:
- Remove the package from the
composer.json
file. - Update the
composer.lock
file. - Uninstall the package files from the
vendor
directory.
2. Remove Service Providers or Aliases
If the package added a service provider or alias to your Laravel app, open the config/app.php
file and remove the corresponding entries. For example:
'providers' => [
// Remove the package service provider if it exists
Spatie\Permission\PermissionServiceProvider::class,
],
Also, check for any custom configuration files or bindings the package might have introduced.
3. Clear Cached Configuration
Clear any cached configuration to ensure Laravel doesn’t try to load the removed package:
php artisan config:clear
php artisan cache:clear
4. Remove Any Published Files
If the package published files (e.g., configuration, migrations, assets), delete them manually. For instance:
- Configuration files are usually located in the
config
directory. - Published migrations are in the
database/migrations
directory.
5. Check Your Code for References
Review your codebase to ensure there are no remaining references to the package. This includes:
- Helper methods.
- Facades.
- Classes provided by the package.
6. Test Your Application
Run your application to confirm that everything works as expected after the package removal. You can also run tests to ensure no functionality has been broken.
That’s it! The package should now be completely removed from your Laravel project.
In addition to the steps I mentioned earlier, there are a few additional things you might need to consider depending on the package you are removing:
7. Remove Environment Variables
If the package required environment variables (e.g., in the .env
file), remove them to clean up unused configuration values. For example:
PACKAGE_API_KEY=your-api-key
PACKAGE_SECRET=your-secret
8. Delete Package-Specific Files
Some packages may generate specific files or used storage in your project. Check your directories for files such as:
resources/views/vendor/package-name
public/vendor/package-name
storage/app/package-name
Remove these if they are no longer needed.
9. Remove Package-Specific Database Changes
If the package included database migrations or created custom tables, review your database schema. You may need to:
- Drop custom tables.
- Remove or rollback migrations
Ensure you understand the impact before performing any database modifications.
10. Update Dependencies and Autoload Files
Run the following commands to update and clean up your project’s dependencies:
composer install
composer dump-autoload
This will regenerate the autoload files and ensure no references to the removed package remain.
11. Check for Unused Configurations
Inspect your configuration files (config/
) for any leftover references to the package, and remove them if necessary.
12. Verify Build Processes
If your project uses build tools like Webpack, Vite, or Laravel Mix, check your JavaScript (webpack.mix.js
, vite.config.js
) or CSS files for references to the package. For example, if the package added frontend assets, remove any references like:
mix.js('resources/js/package.js', 'public/js');
13. Run Tests Again
If your project includes automated tests, re-run the test suite to confirm that everything works as expected after the package removal. Fix any failing tests caused by the absence of the package.
By thoroughly reviewing these steps, you can ensure the package is completely removed and your Laravel project remains clean and functional.