 [ ← blog / drupal ](/) Dec 02, 2025  ·  \#drupal  ·  2 min read 

# Preventing a &#96;drush updb&#96; from clearing your caches



By default, `drush updb` clears the cache after applying database updates. For deployments where you want to avoid an unnecessary performance hit, you can prevent this default behavior using a Drush pre-command hook. The `updb` command has a `--cache-clear` flag that you can set up in your CI workflow, but what about local testing? Will you or your team remember to set that flag every time?

I've come to learn this may be a controversial take. This requires having [a deployment identifier](https://mglaman.dev/blog/what-deployment-identifier-drupal) set and crafted update hooks for specific cache invalidations, router rebuilds, etc. But if you want a highly performant Drupal application with faster deployment times, it's crucial.

## The hook

You can register a Drush hook to override the default `--cache-clear` option on the `updb` command. This effectively automates using the `--no-cache-clear` flag.

```php
<?php

declare(strict_types=1);

namespace Drupal\my_module\Drush;

use Drush\Commands\DrushCommands;
use Consolidation\AnnotatedCommand\CommandData;
use Consolidation\AnnotatedCommand\Hooks\HookManager;
use Drush\Attributes as CLI;
use Drush\Commands\core\UpdateDBCommands;

final class MyModuleDrushCommands extends DrushCommands {

  /**
   * Disable the cache clear option for the updb command.
   */
  #[CLI\Hook(type: HookManager::PRE_COMMAND_HOOK, target: UpdateDBCommands::UPDATEDB)]
  public function disableUpdbCacheClear(CommandData $commandData): void {
    $commandData->input()->setOption('cache-clear', FALSE);
  }
}
```

### How It Works

- `<strong>#[CLI\Hook(...)]</strong>`: This attribute registers the method as a Drush hook.
- `<strong>type: HookManager::PRE_COMMAND_HOOK</strong>`: The hook executes before the main `updb` command.
- `<strong>target: UpdateDBCommands::UPDATEDB</strong>`: This limits the hook's execution to only the `updb` command.
- `<strong>$commandData->input()->setOption('cache-clear', FALSE)</strong>`: This line programmatically sets the `--cache-clear` option to `FALSE`, overriding the default `TRUE` value.

For Drush to discover this command class, it must be located in a namespace registered with Drush's auto-discovery mechanism. A typical pattern is to place this code in a custom module's `src/Drush/Commands` directory or in a [global command](https://mglaman.dev/blog/auto-discovery-global-commands-drush) class that lives outside of a module.

Photo by Mahdi Bafande: <https://www.pexels.com/photo/irregular-streaks-of-lights-15629018/>

 

  ![](/themes/custom/mglaman/images/headshot.webp)Thanks for reading

I write about Drupal, PHP, and the quiet infrastructure behind large sites — caching, config, and testing. New posts land every week or two.

 [Subscribe via RSS →](/rss.xml) [About me →](/about-me) 

 



 

  [← Older

AI workflows will reshape development organizations

 ](/blog/ai-workflows-will-reshape-development-organizations) [Newer →

Automating Drupal release notes in Dependabot PRs

 ](/blog/automating-drupal-release-notes-dependabot-prs)