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:collection console command evicts second-level-cache regions for a collection. It operates on $em->getCache(), which only returns a Cache instance when the second-level cache was enabled in the ORM configuration; otherwise it is null and the command throws InvalidArgumentException because there is nothing to clear.

Source

Thrown at src/Tools/Console/Command/ClearCache/CollectionRegionCommand.php:70

<info>%command.name% 'Entities\MyEntity' 'collectionName' --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);
        $ownerClass = $input->getArgument('owner-class');
        $assoc      = $input->getArgument('association');
        $ownerId    = $input->getArgument('owner-id');
        $cache      = $em->getCache();

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

        if (( ! $ownerClass || ! $assoc) && ! $input->getOption('all')) {
            throw new InvalidArgumentException('Missing arguments "--owner-class" "--association"');
        }

        if ($input->getOption('flush')) {
            $cache->getCollectionCacheRegion($ownerClass, $assoc)
                ->evictAll();

            $ui->comment(
                sprintf(
                    'Flushing cache provider configured for <info>"%s#%s"</info>',
                    $ownerClass,
                    $assoc,
                ),
            );

View on GitHub (pinned to d9b9ff7301)

Solutions

  1. If you use the second-level cache, enable it in configuration (doctrine-bundle: orm.second_level_cache with a region cache driver) and re-run the command.
  2. If you do not use it, drop the command from your deploy/CI scripts; nothing is cached to clear.
  3. Guard scripted calls: run the command only when $em->getCache() instanceof Cache.

Example fix

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

# after (if SLCache is wanted) enable it, e.g. doctrine-bundle config:
# doctrine:
#     orm:
#         second_level_cache:
#             region_cache_driver: { type: file_system }
Defensive patterns

Strategy: type-guard

Validate before calling

use Doctrine\ORM\Cache;
use Doctrine\ORM\EntityManager;

function slcConfigured(EntityManager $em): bool
{
    return $em->getCache() instanceof Cache;
}

// before running the command programmatically:
if (slcConfigured($em)) {
    // safe to run orm:clear-cache:region:collection
}

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

When invoking the console command from a script, catch \InvalidArgumentException and degrade to a notice instead of failing the deploy: catch (\InvalidArgumentException $e) { $io->note('Second-level cache not configured, skipping'); return 0; }

Prevention

When it happens

Trigger: Running bin/console orm:clear-cache:region:collection (or doctrine:orm:clear-cache:region:collection via doctrine-bundle) on an EntityManager whose configuration never enabled the second-level cache (no second_level_cache block, no setSecondLevelCacheConfiguration).

Common situations: Symfony apps using doctrine-bundle without an orm.second_level_cache section; deploy scripts that run every cache command unconditionally; copied cache-clear snippets in projects that only use query/result caches.

Related errors


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