I'm trying to do craft()->templateCache->deleteCachesByElementId on entries.onDeleteEntry, unsuccessfully.

The event listener is working, but the caches don't get invalidated.

Here's the code:

public function init()
    craft()->on('entries.onSaveEntry', function($event) {

    craft()->on('entries.onDeleteEntry', function($event) {


public function clearEntryCache(Event $event) {
    $entry = $event->params['entry'];
    craft()->templateCache->deleteCachesByElementId($entry->id, true);
  • entries.onSaveEntry works perfectly.
  • Instead of run deleteCachesByElementId, if I deleteAllCaches, it works (but I don't want to do this)
  • I've also tried entries.onBeforeDeleteEntry with no luck.
  • I've tried deleting from the entry, and from the entry listing (bulk delete) screen.

On the surface it seems maybe it isn't working because by the time deleteCachesByElementId, the element is already deleted...though it still seems there should be a way to delete that cache with the elementId reference.

  • Do you have stuck tasks, by chance? (buildwithcraft.com/help/stuck-tasks) A difference between deleteCachesByElementId and deleteAllCaches is that the former regulates the bulk of it's work to a task to run.
    – Brad Bell
    Sep 16 '15 at 0:59
  • No stuck tasks (craft_tasks is empty)
    – Tim Kelty
    Sep 16 '15 at 2:06
  • Just double checked - no errors in logs
    – Tim Kelty
    Sep 16 '15 at 2:09
  • By the time the onDeleteEntry event is raised, then the entry is already deleted and it's caches should already be nuked. That's not what you're seeing? Maybe you've got some other caching going on (browser/Varnish/PHP opcache/etc.)?
    – Brad Bell
    Sep 16 '15 at 22:58
  • Ah yes - I see that in ElementsService.php:1669...Interesting, I see saveElement does that too...so now I'm wondering why I had to manually do that. So - keep in mind this is template output, but coming from a plugin not a craft.entries tag.
    – Tim Kelty
    Sep 18 '15 at 14:14

Ok so here's what did it: In my plugin, I had to manually call:


With that, I didn't need had any of my event listeners in place.

Interesting that things seem to be inserted into craft_templatecachecriteria automatically, but not into craft_templatecacheelements...Perhaps that is bug?

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.