9

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), 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.

  • 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 Sep 23 '14 at 20:47
  • @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 Sep 23 '14 at 20:48
  • I'm going with Fred's answer, then. :) – Brad Bell Sep 23 '14 at 20:58
7

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';
  • Perfect, thank you for the great answer Fred! – Matt Stein Sep 23 '14 at 21:01
3

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.