Cleaning Up composer.json Before a Drupal Upgrade: Dead Modules, Their Dependencies, and Devel in Production

By Lars Nielsen ·

Before upgrading this site to Drupal 11, I spent an afternoon in August making composer.json describe the site that actually exists. Over the years it had collected modules and themes that were no longer installed, and a development module that was being shipped to production. Five small commits fixed that. None of them changed what the site does, and together they removed a few thousand lines from composer.lock and the codebase. Here's the order that kept it safe, and what each step found.

The rule: uninstall in Drupal first, remove in Composer second

Drupal's documentation spells out the order. Since Drupal 8 there's no "disable": a module you don't need has to be uninstalled, which also removes its configuration, and only after that should its code go away. Removing the code of a module that's still installed leaves Drupal referring to something that no longer exists.

So each removal started from an extension that was already uninstalled, checked where in doubt with drush pm:list, and only then removed the Composer package. After each one came a cache rebuild, a clean config:status and a check that the site responded normally, plus database updates or a run of the publishing pipeline where the change called for it.

What went

  • Drupal Commerce, long uninstalled. composer remove drupal/commerce also took away five packages that were only there because of it: commerceguys/intl, drupal/entity, drupal/entity_reference_revisions, drupal/profile and drupal/state_machine. That's the hidden part of a dead dependency: it keeps its own dependencies alive too.
  • Three themes, Classy, Stable and Showcase Lite, uninstalled earlier along with their configuration.
  • Blazy, Slick and Inline Entity Form, none of them installed. Slick also had a leftover installer-paths entry for the slick-carousel JavaScript library, which no longer had anything to install.
  • Two custom modules, an old crawler and indexer, whose code I deleted directly. One of them turned out to be still listed as installed in the site's exported configuration, even though production's live configuration had dropped it long ago. Re-exporting the configuration fixed the mismatch, and took a job definition for the module's cron task with it.

That last one is the case where the order rule earns its keep: the code was gone, and the configuration in the repository still said the module was installed. The repository's configuration was describing a site with a module that no longer existed. I wrote more about how a module can be uninstalled but not gone in a separate article.

Devel was going to production

The one change that mattered for the live site was Devel. It sat in require next to the real runtime dependencies, so every composer install on production installed it. It was disabled there, but its code was on the server.

The fix moved it to a new require-dev section. Composer's documentation describes the --no-dev option as skipping the packages listed there, and production installs with composer install --no-dev. After deploying, Devel was gone from production's codebase, while local development, which doesn't use --no-dev, kept it.

The commit edited composer.json by hand. The same change with Composer's own commands, plus the one that answers "why is this package still here?":

# What still requires this package, all the way up?
composer why --tree drupal/entity

# Move a development module out of the production dependencies
composer remove drupal/devel
composer require --dev drupal/devel:^5.0

# On production: install without anything in require-dev
composer install --no-dev

That deploy has a detail worth remembering on any site where production isn't a git checkout: composer.json and composer.lock have to change on both sides, and the install on production is where --no-dev actually takes effect. A require-dev section in the repository does nothing for a server that installs without that flag.

Why bother before an upgrade

Every package in composer.json is a constraint the dependency solver has to satisfy when you change Drupal's major version. Dead packages still have version constraints, and an abandoned or outdated one can block the upgrade of everything else. Removing them first meant the Drupal 11 upgrade only had to deal with code the site really used. I described what was left to solve in the article about an abandoned package Drupal 11 still needed.

Filed under

Leave a comment

Comments are reviewed before they appear.

Basic HTML allowed: links, emphasis, code and lists. Web addresses become links automatically.