doctrine/orm · error · InvalidArgumentException

No second-level cache is configured on the given EntityManag

Error message

No second-level cache is configured on the given EntityManager.

What it means

The orm:clear-cache:region:entity console command evicts a second-level-cache region for one entity class. It needs the EntityManager's second-level cache ($em->getCache()); when the second-level cache was never enabled, that returns null and the command throws InvalidArgumentException because there are no entity regions to evict.

Source

Thrown at src/Tools/Console/Command/ClearCache/EntityRegionCommand.php:68

<info>%command.name% 'Entities\MyEntity' --flush</info>

Finally, be aware that if <info>--flush</info> option is passed,
not all cache providers are able to flush entries, because of a limitation of its execution nature.
EOT);
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $ui = (new SymfonyStyle($input, $output))->getErrorStyle();

        $em          = $this->getEntityManager($input);
        $entityClass = $input->getArgument('entity-class');
        $entityId    = $input->getArgument('entity-id');
        $cache       = $em->getCache();

        if (! $cache instanceof Cache) {
            throw new InvalidArgumentException('No second-level cache is configured on the given EntityManager.');
        }

        if (! $entityClass && ! $input->getOption('all')) {
            throw new InvalidArgumentException('Invalid argument "--entity-class"');
        }

        if ($input->getOption('flush')) {
            $cache->getEntityCacheRegion($entityClass)
                ->evictAll();

            $ui->comment(sprintf('Flushing cache provider configured for entity named <info>"%s"</info>', $entityClass));

            return 0;
        }

        if ($input->getOption('all')) {
            $ui->comment('Clearing <info>all</info> second-level cache entity regions');

View on GitHub (pinned to d9b9ff7301)

Solutions

  1. Enable the second-level cache where you expect to clear it (doctrine-bundle: orm.second_level_cache).
  2. Drop the command from environments that do not use the second-level cache.
  3. Guard the invocation with $em->getCache() instanceof Cache before shelling out.

Example fix

# before
$ php bin/console doctrine:orm:clear-cache:region:entity "App\Entity\User"
# InvalidArgumentException: No second-level cache is configured...

# after: enable SLCache where the command should work, or guard the script:
if ($em->getCache() instanceof \Doctrine\ORM\Cache) {
    // run orm:clear-cache:region:entity
}
Defensive patterns

Strategy: type-guard

Validate before calling

use Doctrine\ORM\Cache;

if (! $em->getCache() instanceof Cache) {
    // no second-level cache: nothing to clear, skip the command
    return 0;
}
// safe to run orm:clear-cache:region:entity

Type guard

use Doctrine\ORM\Cache;
use Doctrine\ORM\EntityManagerInterface;

/** @psalm-assert Cache $cache */
function assertSlcCache(EntityManagerInterface $em): void
{
    $cache = $em->getCache();
    assert($cache instanceof Cache);
}

Try / catch

Catch \InvalidArgumentException around the command call and treat it as a configuration signal: log 'second-level cache not configured in this environment' and continue the deploy.

Prevention

When it happens

Trigger: Running orm:clear-cache:region:entity 'App\Entity\User' on an app whose ORM configuration has no second-level cache (no second_level_cache block / setSecondLevelCacheConfiguration); running it against an EntityManager built from a bare Configuration in a worker or CLI kernel.

Common situations: Deploy scripts that run every cache command; projects that enabled only query/result caches; environments where SLCache is on in prod but off in staging while the same script runs in both.

Related errors


AI-assisted analysis of doctrine/orm@d9b9ff7301 (2026-08-21). Data as JSON: /api/errors/a3b40894f1af7297. Report an issue: GitHub.