Question

How to Remove a Package in Laravel with Composer

phplaravelcomposer-phpuninstallationpackage-managers

Question

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

I have tried the following steps:

  • Removing the package declaration from composer.json under the require section
  • Removing any class aliases from app.php
  • Removing any references to the package from my code
  • Running composer update
  • Running composer dump-autoload

However, the package still does not seem to be fully removed. What step am I missing?

Short Answer

By the end of this page, you will understand how Composer removes packages in a Laravel project, why manually editing composer.json is often not enough, and which Laravel-specific cleanup steps may also be required. You will also learn the correct commands, common mistakes, and how to verify that a package has actually been removed.

Concept

Composer is the dependency manager for PHP projects, including Laravel applications. Its job is to install, update, and remove packages listed in composer.json, then lock the exact installed versions in composer.lock.

When you want to remove a package, the most reliable approach is not to manually delete it from composer.json. Instead, you should use Composer's built-in remove command:

composer remove vendor/package

This matters because Composer does more than just edit one file:

  • It removes the package from composer.json
  • It updates composer.lock
  • It removes the package from the vendor/ directory if no longer needed
  • It regenerates autoload files

In Laravel, there may also be framework-specific integration points to clean up, such as:

  • Service providers in config/app.php or older app.php setups
  • Class aliases
  • Published configuration files
  • Views, migrations, or assets copied into your project
  • Cached configuration or route files

If you only delete a line from composer.json, the package can still appear present because:

  • It may still exist in composer.lock
  • It may still be installed in vendor/
  • Laravel may still have cached references to it
  • Your app may still load manually published files or configuration

So the underlying concept is: dependency removal is a package-manager operation, not just a file edit.

Mental Model

Think of Composer as a warehouse manager.

  • composer.json is the shopping list
  • composer.lock is the exact receipt of what was actually bought
  • vendor/ is the storage room containing the packages
  • Laravel config and cache are like labels and notes taped around your app

If you only erase an item from the shopping list, that does not automatically clean the storage room or update the receipt. Using:

composer remove vendor/package

is like telling the warehouse manager to:

  1. Remove the item from the list
  2. Update the receipt
  3. Take it out of storage
  4. Re-label everything correctly

Then in Laravel, you may also need to remove the extra notes and labels, such as service providers, aliases, and cached config.

Take Quiz

Syntax and Examples

The standard syntax is:

composer remove vendor/package

Example

Suppose your Laravel app uses this package:

{
  "require": {
    "laravel/framework": "^10.0",
    "barryvdh/laravel-debugbar": "^3.9"
  }
}

To remove it, run:

composer remove barryvdh/laravel-debugbar

Composer will:

  • Remove it from composer.json
  • Update composer.lock
  • Remove its files from vendor/
  • Rebuild autoload files

Laravel cleanup example

If your project manually registered the package in config/app.php, you may also need to remove entries like these:

'providers' => [
    Barryvdh\Debugbar\ServiceProvider::class,
],

'aliases' => [
    'Debugbar' => Barryvdh\Debugbar\Facade::class,
],

After removing those references, clear Laravel caches:

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

Verifying removal

You can check whether Composer still knows about the package:

composer show barryvdh/laravel-debugbar

If it is removed, Composer will usually report that the package is not installed.

Step by Step Execution

Consider this command:

composer remove vendor/package

Here is what happens step by step:

  1. Composer reads your composer.json
  2. It finds vendor/package in your dependencies
  3. It removes that dependency entry
  4. It recalculates the dependency graph
  5. It updates composer.lock to match the new dependency set
  6. It deletes package files from vendor/ if no remaining package needs them
  7. It regenerates the autoloader

Small example

Imagine your composer.json contains:

{
  "require": {
    "laravel/framework": "^10.0",
    "vendor/package": "^1.0"
  }
}

You run:

composer remove vendor/package

After that:

  • vendor/package is removed from composer.json
  • composer.lock is rewritten
  • the package directory under vendor/ is removed
  • autoload mappings are regenerated

Then Laravel may still need cleanup

If the package had been manually wired into Laravel, you should also:

php artisan optimize:clear

This clears common caches that may still reference removed classes or configuration.

If the package published files into your project, those files do not get automatically deleted. You must remove them manually if they are no longer needed.

Real World Use Cases

Removing packages is common in real Laravel projects.

1. Replacing one package with another

You may uninstall a logging, image, payment, or admin package before switching to a new one.

2. Reducing app size and complexity

A package added during experimentation may no longer be needed in production.

3. Fixing dependency conflicts

A package may block upgrades to a newer Laravel or PHP version.

4. Removing development tools

You might remove debugging or profiling packages from a deployed application.

5. Security and maintenance cleanup

If a package is abandoned or vulnerable, it is often better to remove it entirely.

6. Simplifying deployment

Fewer dependencies usually mean faster installs, smaller vendor directories, and fewer integration points to maintain.

Take Quiz

Real Codebase Usage

In real projects, developers usually remove packages in a few layers.

Typical pattern

  1. Remove the package with Composer
  2. Remove framework registration code
  3. Remove package-specific config or published files
  4. Clear caches
  5. Run tests

Common Laravel patterns

Guard clause before cleanup

Teams often first verify whether the package is direct or transitive:

composer why vendor/package

If another package depends on it, you may not be able to remove it directly.

Configuration cleanup

Older Laravel apps may register providers and aliases manually. Newer apps often use package auto-discovery, so there may be less manual cleanup.

Cache clearing

After removing package classes, cached config may still point to them. That can trigger errors such as class not found.

Validation through tests

After removal, developers run:

php artisan test

or:

vendor/bin/phpunit

This confirms no remaining code depends on the removed package.

Published assets and migrations

Some packages copy files into your project. Composer removes package code from vendor/, but it does not automatically delete files already published into:

  • config/
  • database/migrations/
  • public/
  • resources/views/

Those should be reviewed manually.

Common Mistakes

1. Editing composer.json manually instead of using composer remove

This is the most common issue.

Less reliable approach

{
  "require": {
    "laravel/framework": "^10.0"
  }
}

If you only delete the package line and do not let Composer manage the full removal process, your lock file and installed packages may become inconsistent.

Better approach

composer remove vendor/package

2. Forgetting Laravel service providers or aliases

Broken example:

'providers' => [
    Vendor\Package\ServiceProvider::class,
],

If the package is removed but this line stays, Laravel may fail during boot.

3. Forgetting cached configuration

A package may be gone, but Laravel still uses old cached config.

Fix:

php artisan optimize:clear

4. Assuming published files are removed automatically

Composer only manages dependencies. It does not usually remove files that were copied into your app earlier.

Examples:

  • config/package.php
  • package migrations
  • public assets

5. Removing a transitive dependency directly

If package A depends on package B, removing B may not work because A still requires it.

Check with:

composer why vendor/package

6. Forgetting to search the codebase

Even after successful removal, your code may still reference the package's classes, facades, helpers, or config keys.

Use your editor or command-line search to find leftover references.

7. Running composer update when composer remove is the right tool

composer update updates versions. composer remove expresses your intent clearly: uninstall this dependency.

Comparisons

TaskRecommended CommandWhy
Remove a packagecomposer remove vendor/packageUpdates composer.json, composer.lock, vendor files, and autoload
Install a packagecomposer require vendor/packageAdds dependency correctly and installs it
Update dependenciescomposer updateRefreshes versions based on constraints
Rebuild autoload files onlycomposer dump-autoloadUseful for autoload issues, but does not remove dependencies

Manual edit vs Composer command

ApproachProsCons
Manually edit composer.jsonQuick for inspectionEasy to leave lock file or vendor state inconsistent
composer removeSafe, explicit, completeRequires correct package name

Laravel auto-discovery vs manual registration

Setup StyleWhat to remove
Auto-discovered packageOften only composer remove, plus any published files or app code references
Manually registered packageRemove Composer dependency, provider entries, aliases, and possibly config

composer remove vs composer dump-autoload

CommandRemoves package?Updates lock file?Rebuilds autoload?
composer remove vendor/packageYesYesYes
composer dump-autoloadNoNoYes
Take Quiz

Cheat Sheet

# Remove a package properly
composer remove vendor/package

# Check whether a package is still installed
composer show vendor/package

# See why a package exists in the project
composer why vendor/package

# Clear Laravel caches after removal
php artisan optimize:clear

Rules to remember

  • Prefer composer remove over manual editing
  • Remove Laravel service providers and aliases if manually registered
  • Clear Laravel caches after removing package classes
  • Check for published files in config/, public/, resources/, and database/
  • Search your codebase for leftover references
  • Use composer why if the package seems impossible to remove

Common edge cases

  • The package is still required by another package
  • The package used Laravel auto-discovery, so there may be no provider to remove manually
  • Config cache may still reference deleted classes
  • Published files remain even after the package is removed

FAQ

How do I uninstall a Laravel package correctly?

Use:

composer remove vendor/package

Then remove Laravel-specific references such as service providers, aliases, and published files if needed.

Why is deleting the package from composer.json not enough?

Because Composer also tracks dependencies in composer.lock and the vendor/ directory. Manual deletion does not fully synchronize everything.

Do I need to run composer dump-autoload after composer remove?

Usually no. composer remove already regenerates autoload files.

Why does Laravel still try to load the removed package?

Most often because of cached config, a remaining service provider, an alias, or leftover code references.

How can I check whether another package still depends on it?

Run:

composer why vendor/package

Does Composer delete published config files and migrations?

No. Files copied into your Laravel project usually need to be removed manually.

What command should I run after removing a package in Laravel?

A good default is:

php artisan optimize:clear

Then run your tests or start the app to confirm everything works.

Take Quiz

Mini Project

Description

Create a small Laravel cleanup checklist for uninstalling a package safely. This project demonstrates the full removal workflow: Composer removal, Laravel cleanup, cache clearing, and verification. It is useful because package removal in real projects often fails due to leftover configuration or cached references rather than Composer itself.

Goal

Build a repeatable process for removing a Laravel package and verifying that your application no longer depends on it.

Requirements

  1. Choose a package currently installed in a Laravel project or use a sample package name.
  2. Remove the package using the correct Composer command.
  3. Remove any manual service provider or alias registrations.
  4. Clear Laravel caches after removal.
  5. Verify that the package is no longer installed and no code references remain.
Take Quiz

Keep learning

Related questions

View more