{"record":{"id":"a3b40894f1af7297","repo":"doctrine/orm","slug":"no-second-level-cache-is-configured-on-the-given-e-a3b408","errorCode":null,"errorMessage":"No second-level cache is configured on the given EntityManager.","messagePattern":"No second-level cache is configured on the given EntityManager\\.","errorType":"console","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Tools/Console/Command/ClearCache/EntityRegionCommand.php","lineNumber":68,"sourceCode":"\n<info>%command.name% 'Entities\\MyEntity' --flush</info>\n\nFinally, be aware that if <info>--flush</info> option is passed,\nnot all cache providers are able to flush entries, because of a limitation of its execution nature.\nEOT);\n    }\n\n    protected function execute(InputInterface $input, OutputInterface $output): int\n    {\n        $ui = (new SymfonyStyle($input, $output))->getErrorStyle();\n\n        $em          = $this->getEntityManager($input);\n        $entityClass = $input->getArgument('entity-class');\n        $entityId    = $input->getArgument('entity-id');\n        $cache       = $em->getCache();\n\n        if (! $cache instanceof Cache) {\n            throw new InvalidArgumentException('No second-level cache is configured on the given EntityManager.');\n        }\n\n        if (! $entityClass && ! $input->getOption('all')) {\n            throw new InvalidArgumentException('Invalid argument \"--entity-class\"');\n        }\n\n        if ($input->getOption('flush')) {\n            $cache->getEntityCacheRegion($entityClass)\n                ->evictAll();\n\n            $ui->comment(sprintf('Flushing cache provider configured for entity named <info>\"%s\"</info>', $entityClass));\n\n            return 0;\n        }\n\n        if ($input->getOption('all')) {\n            $ui->comment('Clearing <info>all</info> second-level cache entity regions');\n","sourceCodeStart":50,"sourceCodeEnd":86,"githubUrl":"https://github.com/doctrine/orm/blob/d9b9ff73016bf598ae07515f97289ce8074e97a5/src/Tools/Console/Command/ClearCache/EntityRegionCommand.php#L50-L86","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Enable the second-level cache where you expect to clear it (doctrine-bundle: orm.second_level_cache).","Drop the command from environments that do not use the second-level cache.","Guard the invocation with $em->getCache() instanceof Cache before shelling out."],"exampleFix":"# before\n$ php bin/console doctrine:orm:clear-cache:region:entity \"App\\Entity\\User\"\n# InvalidArgumentException: No second-level cache is configured...\n\n# after: enable SLCache where the command should work, or guard the script:\nif ($em->getCache() instanceof \\Doctrine\\ORM\\Cache) {\n    // run orm:clear-cache:region:entity\n}","handlingStrategy":"type-guard","validationCode":"use Doctrine\\ORM\\Cache;\n\nif (! $em->getCache() instanceof Cache) {\n    // no second-level cache: nothing to clear, skip the command\n    return 0;\n}\n// safe to run orm:clear-cache:region:entity","typeGuard":"use Doctrine\\ORM\\Cache;\nuse Doctrine\\ORM\\EntityManagerInterface;\n\n/** @psalm-assert Cache $cache */\nfunction assertSlcCache(EntityManagerInterface $em): void\n{\n    $cache = $em->getCache();\n    assert($cache instanceof Cache);\n}","tryCatchPattern":"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.","preventionTips":["Run region-clear commands only in environments where SLCache is enabled.","Share the cache configuration between web and CLI kernels.","Include an SLCache-enabled environment in CI for deploy script tests."],"tags":["doctrine-orm","second-level-cache","console","cache","symfony","cli"],"backgroundTag":"missing-cache-configuration","analyzedSha":"d9b9ff73016bf598ae07515f97289ce8074e97a5","analyzedAt":"2026-08-21T06:13:15.863Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}