Skip to content
Latchkey

Drupal "The website encountered an unexpected error" (stale cache) in CI

Drupal compiles services and routes into a cached container. After updating code, modules, or config in CI, the old cache can reference a class or service that changed, and Drupal aborts the request with a generic unexpected-error message until you rebuild.

What this error means

A page render or post-deploy check fails with "The website encountered an unexpected error. Please try again later." and the log shows a "ServiceNotFoundException" or "Class ... not found" after an update.

Drupal
The website encountered an unexpected error. Please try again later.
Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException:
The service "my_module.manager" has a dependency on a non-existent service "old.service".

Common causes

A stale compiled container after a code change

The cached service container still references services or classes that an update renamed or removed, so resolution fails at runtime.

Caches not rebuilt after enabling or updating modules

New routes, plugins, or services are not picked up until the cache is cleared, so requests hit the old definitions.

How to fix it

Rebuild caches after every update

  1. Run drush cache:rebuild after composer install and drush updatedb.
  2. Order it before any request-serving check.
  3. Re-run the failing page to confirm the new container loads.
Terminal
./vendor/bin/drush updatedb -y
./vendor/bin/drush cache:rebuild

Clear caches before config import

Rebuild so the container matches current code before importing configuration that depends on new services.

Terminal
./vendor/bin/drush cr && ./vendor/bin/drush config:import -y

How to prevent it

  • Always run drush cache:rebuild after deploy-time updates.
  • Sequence updatedb, cache rebuild, then config import.
  • Treat the compiled container as disposable across deploys.

Frequently asked questions

What causes ""The website encountered an unexpected error""?
The cached service container still references services or classes that an update renamed or removed, so resolution fails at runtime.
How do I fix "The website encountered an unexpected error"?
Rebuild caches after every update

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card