<p>I've had a quick look at how this is done in Craft's own <a href="https://github.com/craftcms/cms/blob/4679c14bc529071fb8214d1dacb537e76a3a0a01/src/controllers/UtilitiesController.php#L199" rel="nofollow noreferrer">Utilities controller</a>. I think you should be able to clear the template cache with this code:</p>
<pre><code>$this-&gt;requirePermission('utility:clear-caches');

$cache = Craft::$app-&gt;getCache();
TagDependency::invalidate($cache, 'template');
</code></pre>

CC BY-SA 4.0
2024-06-28T12:44:31.587Z
2024-06-28T12:44:31.587Z
Martin Spain
<blockquote>
<p>As far as I know there is also no fitting CLI command</p>
</blockquote>
<p>No, there is! Check out the <a href="https://craftcms.com/docs/4.x/console-commands.html#invalidate-tags-template" rel="nofollow noreferrer"><code>invalidate-tags/template</code></a> command.</p>

CC BY-SA 4.0
2024-07-02T19:38:24.097Z
2024-07-02T19:38:24.097Z
Mats Mikkel Rummelhoff
<p>Is there a way to clear the template cache via a plugin? I didn't find a way to only clear the template cache and keep the rest of the cache intact. As far as I know there is also no fitting CLI command. I'm currently working in Craft 4.</p>

<plugin-development><cache><craft4>
Clear template cache from inside of a plugin
# Clear template cache from inside of a plugin

(Asked by: username_0 on 2024-06-28)

Is there a way to clear the template cache via a plugin? I didn't find a way to only clear the template cache and keep the rest of the cache intact. As far as I know there is also no fitting CLI command. I'm currently working in Craft 4.

**Tags:** plugin-development, cache, craft4


---

## 2 answers

### Answer by username_1 on 2024-06-28. Score: 2 | Accepted answer

I've had a quick look at how this is done in Craft's own Utilities controller. I think you should be able to clear the template cache with this code:
$this->requirePermission('utility:clear-caches');

$cache = Craft::$app->getCache();
TagDependency::invalidate($cache, 'template');


---

### Answer by username_2 on 2024-07-02. Score: 1

> As far as I know there is also no fitting CLI command

No, there is! Check out the [`invalidate-tags/template`](https://craftcms.com/docs/4.x/console-commands.html#invalidate-tags-template) command.


---

