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
orm:clear-cache:region:query evicts a second-level-cache query region (the default region when no region name is given). Like the other region commands it requires the second-level cache to be enabled: $em->getCache() returns null otherwise, and the command throws InvalidArgumentException because there are no query regions to evict.
Source
Thrown at src/Tools/Console/Command/ClearCache/QueryRegionCommand.php:70
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);
$name = $input->getArgument('region-name');
$cache = $em->getCache();
if ($name === null) {
$name = Cache::DEFAULT_QUERY_REGION_NAME;
}
if (! $cache instanceof Cache) {
throw new InvalidArgumentException('No second-level cache is configured on the given EntityManager.');
}
if ($input->getOption('flush')) {
$cache->getQueryCache($name)
->getRegion()
->evictAll();
$ui->comment(
sprintf(
'Flushing cache provider configured for second-level cache query region named <info>"%s"</info>',
$name,
),
);
return 0;
}
if ($input->getOption('all')) {View on GitHub (pinned to d9b9ff7301)
Solutions
- Enable the second-level cache (doctrine-bundle: orm.second_level_cache) where you intend to clear query regions.
- Remove the command from environments that do not use the second-level cache.
- Guard scripted runs with $em->getCache() instanceof Cache.
Example fix
# before $ php bin/console doctrine:orm:clear-cache:region:query my_region # InvalidArgumentException: No second-level cache is configured... # after: enable SLCache, then $ php bin/console doctrine:orm:clear-cache:region:query my_region
Defensive patterns
Strategy: type-guard
Validate before calling
use Doctrine\ORM\Cache;
if (! $em->getCache() instanceof Cache) {
// second-level cache not enabled: no query regions to evict
return 0;
}
// safe to run orm:clear-cache:region:query [region] 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 invocation in scripts and downgrade to an informational note ('SLCache not enabled here') rather than a pipeline failure. Prevention
- Only schedule region-clear commands where SLCache is enabled.
- If queries use setCacheable(true), ensure the second-level cache is configured too.
- Keep region names in configuration constants so command arguments never drift.
When it happens
Trigger: Running orm:clear-cache:region:query [region-name] on an EntityManager whose configuration never enabled the second-level cache; running it in an environment (dev/staging) where SLCache is off while prod has it on.
Common situations: Deploy scripts copied between environments; projects using ->enableResultCache()/setCacheable(true) without enabling the second-level cache; setup code that clears regions before SLCache is configured.
Related errors
- No second-level cache is configured on the given EntityManag
- No second-level cache is configured on the given EntityManag
- Missing arguments "--owner-class" "--association"
- Invalid argument "--entity-class"
- No Metadata cache driver is configured on given EntityManage
AI-assisted analysis of doctrine/orm@d9b9ff7301 (2026-08-21).
Data as JSON: /api/errors/049b03afbef43eda.
Report an issue: GitHub.