Is it possible to clear caches and re-index assets programattically? When migrating a Craft site, I'm used to...


deploying files
checking my config, tweaking as necessary
loading a database backup
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), 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.
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()-&gt;db-&gt;createCommand()-&gt;truncateTable('assettransformindex');
craft()-&gt;db-&gt;createCommand()-&gt;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()-&gt;db-&gt;createCommand()-&gt;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';