Flush pages cache via cli - wrong root

When I flush the pages cache via cli it doesn’t clear the cache.

Test Setup

  1. Fresh startkit
  2. Turn on Cache in config.php
  3. Open the website via browser to fill the cache
  4. Confirm that the cache folder was created and a cache file exists. In my case site/cache/localhost_startkit/pages/home.html.cache
  5. Create flush.php in your root:
<?php
require 'kirby/bootstrap.php';

$kirby = new Kirby();
$kirby->cache('pages')->flush();
  1. Run php flush.php on the command line
  2. Check the cache folder ls site/cache_ localhost_starterkit. A new folder named _ got created and the cached page is still there: site/cache/localhost_startkit/pages/home.html.cache.

Debugging

dump($this->root()); in the flush method kirby/src/Cache/FileCache.php:160 yields [...]/cache/_/pages.

So the root, or more specific the prefix is incorrect. How would Kirby know the domain name though? I can’t think of a way for Kirby to detect it since everything is running via cli.

On the other hand, @bnomei somehow gets flush to work from the cli with janitor.

This came up recently in another thread. The solution was to set the url in config.

1 Like

The url option indeed fixes flushing the cache via CLI. Thank you!

(Still curious how @bnomei managed to make it work without the option)

I think he doesn’t use the Kirby Cache functions, but instead flushes everything via the file system: https://github.com/bnomei/kirby3-janitor/blob/master/classes/CleanCacheFilesJob.php

2 Likes

You’re right! I was looking at FlushPagesCacheJob.php which is doing the basic kirby()->cache('pages')->flush() but when I was testing it I was actually calling cleanCache which resolves to the class you’re referring to. Thank you!

As this post helped me sort out the problem, I would like to share my findings, so: For anyone, who comes accross this post and has trouble to get things to work with multiple environments, maybe my post in this related topic can help.