<h2>Updated</h2>
<p>Craft version 3+ now has console commands built in that can handle these tasks.</p>
<pre><code>$ php craft clear-caches/all
$ php craft index-assets/all
</code></pre>
<p>See documentation for <a href="https://craftcms.com/docs/3.x/console-commands.html" rel="nofollow noreferrer">Console Commands in 3.x</a> or <a href="https://craftcms.com/docs/4.x/console-commands.html" rel="nofollow noreferrer">Console Commands in 4.x</a></p>
<h2>Craft v2</h2>
<p>Take a look at <a href="https://github.com/rsanchez/craft-cli" rel="nofollow noreferrer">Craft CLI</a>. It's a command line interface for Craft.</p>
<p>It let's you run commands like <code>craft clear:cache</code> and <code>craft update:assetsindexes</code> from the command line.</p>

CC BY-SA 4.0
2016-12-15T22:26:56.677Z
2022-06-19T02:59:19.993Z
2022-06-19T02:59:19.993Z
Alex Roper
<p>I guess it depends. So, behind the scenes, Craft can have different cache drivers. You might use the db or file cache, or something like Redis or Memcache.</p>

<p>Took a peek at ClearCachesTool, and it looks like it's doing the things you mentioned:</p>

<pre><code>craft()-&gt;db-&gt;createCommand()-&gt;truncateTable('assettransformindex');
craft()-&gt;db-&gt;createCommand()-&gt;truncateTable('assetindexdata');
</code></pre>

<p>As well as deleting all the internal cache folders (runtime/cache, assets, compiled_templates, temp), including any plugin cache paths that have registered via <strong>registerCachePaths</strong>.</p>

<pre><code>craft()-&gt;db-&gt;createCommand()-&gt;delete(static::$_templateCachesTable);
</code></pre>

<p>It also deletes the templates cache.</p>

<p>I guess what you're looking for are these:</p>

<ul>
<li>CacheService->flush()</li>
<li>ClearCachesTool->performAction() (does most of the clearing)</li>
<li>craft()->templateCache->deleteAllCaches(); (called in ClearCachesTool - TemplateCacheService)</li>
</ul>

<p>So yes, you can either bootstrap Craft and call a php file from the commandline, or add a plugin with an action that you GET/POST to after deployment.</p>

<p>The former, you can do with the included bootstrap.php file (as of 2.2)</p>

<pre><code>$craft = require 'craft/app/bootstrap.php';
</code></pre>

CC BY-SA 3.0
2014-09-23T21:01:46.420Z
Perfect, thank you for the great answer Fred!
Matt Stein
CC BY-SA 3.0
2014-09-23T20:55:40.873Z
2014-09-23T20:55:40.873Z
Fred Carlsen
<p>When migrating a Craft site, I'm used to...</p>

<ol>
<li>deploying files</li>
<li>checking my config, tweaking as necessary</li>
<li>loading a database backup</li>
<li>running "Update Asset Indexes" and "Clear Caches" from the control panel</li>
</ol>

<p>That last step is always important because random oddities and broken images are fixed site-wide because of caching and transforms. Now I'm attempting to automate the process with a Docker deploy script, so I'm wondering: <strong>is there any way to Update Asset Indexes and Clear Caches programmatically?</strong></p>

<p>I know that you can nuke the <code>craft_assettransformindex</code> table and delete the contents of <code>/craft/storage/runtime/assets</code> and <code>/craft/storage/runtime/cache</code> (<a href="https://craftcms.stackexchange.com/questions/117/is-there-a-way-to-force-assets-to-redo-transforms/119#119">related question</a>), but is that really doing the exact same thing?</p>

<p><em>Note that by "programmatically," I generally mean anything you could achieve from a Linux terminal: MySQL commands, PHP or shell scripts, etc.</em></p>

CC BY-SA 3.0
2014-09-23T20:47:59.770Z
Which caches, specifically, do you want to clear?  The clear caches tool has options for the various different places caching is occurring in Craft.
Brad Bell
CC BY-SA 3.0
2014-09-23T20:48:37.813Z
@BradBell Amended answer: ideally **all** caches, but realistically only those that'd be kept in MySQL since the filesystem would be squeaky clean on deploy—no physical transforms present and no additions to /craft/storage/runtime/assets.
Matt Stein
CC BY-SA 3.0
2014-09-23T20:58:14.150Z
I'm going with Fred's answer, then. :)
Brad Bell
<assets><cache><image-transforms><deployment>
Is it possible to clear caches and re-index assets programattically?
# Is it possible to clear caches and re-index assets programattically?

(Asked by: username_0 on 2014-09-23)

When migrating a Craft site, I'm used to...

1. deploying files
2. checking my config, tweaking as necessary
3. loading a database backup
4. running "Update Asset Indexes" and "Clear Caches" from the control panel

That last step is always important because random oddities and broken images are fixed site-wide because of caching and transforms. Now I'm attempting to automate the process with a Docker deploy script, so I'm wondering: **is there any way to Update Asset Indexes and Clear Caches programmatically?**

I know that you can nuke the `craft_assettransformindex` table and delete the contents of `/craft/storage/runtime/assets` and `/craft/storage/runtime/cache` ([related question](https://craftcms.stackexchange.com/questions/117/is-there-a-way-to-force-assets-to-redo-transforms/119#119)), but is that really doing the exact same thing?

*Note that by "programmatically," I generally mean anything you could achieve from a Linux terminal: MySQL commands, PHP or shell scripts, etc.*

**Tags:** assets, cache, image-transforms, deployment

> **Comments:**
> **username_1**: Which caches, specifically, do you want to clear?  The clear caches tool has options for the various different places caching is occurring in Craft.  
> **username_0**: @BradBell Amended answer: ideally **all** caches, but realistically only those that'd be kept in MySQL since the filesystem would be squeaky clean on deploy—no physical transforms present and no additions to /craft/storage/runtime/assets.  
> **username_1**: I'm going with Fred's answer, then. :)  

---

## 2 answers

### Answer by username_3 on 2014-09-23. Score: 8 | Accepted answer

I guess it depends. So, behind the scenes, Craft can have different cache drivers. You might use the db or file cache, or something like Redis or Memcache.
Took a peek at ClearCachesTool, and it looks like it's doing the things you mentioned:
craft()->db->createCommand()->truncateTable('assettransformindex');
craft()->db->createCommand()->truncateTable('assetindexdata');

As well as deleting all the internal cache folders (runtime/cache, assets, compiled_templates, temp), including any plugin cache paths that have registered via registerCachePaths.
craft()->db->createCommand()->delete(static::$_templateCachesTable);

It also deletes the templates cache.
I guess what you're looking for are these:

CacheService->flush()
ClearCachesTool->performAction() (does most of the clearing)
craft()->templateCache->deleteAllCaches(); (called in ClearCachesTool - TemplateCacheService)

So yes, you can either bootstrap Craft and call a php file from the commandline, or add a plugin with an action that you GET/POST to after deployment.
The former, you can do with the included bootstrap.php file (as of 2.2)
$craft = require 'craft/app/bootstrap.php';

> **Comments:**
> **username_0**: Perfect, thank you for the great answer Fred!  

---

### Answer by username_2 on 2016-12-15. Score: 3

Updated
Craft version 3+ now has console commands built in that can handle these tasks.
$ php craft clear-caches/all
$ php craft index-assets/all

See documentation for Console Commands in 3.x or Console Commands in 4.x
Craft v2
Take a look at Craft CLI. It's a command line interface for Craft.
It let's you run commands like craft clear:cache and craft update:assetsindexes from the command line.


---

