Answers.Body: <p>Ok so here's what did it:
  In my plugin, I had to manually call:</p>
  
  <pre><code>        craft()-&gt;templateCache-&gt;includeElementInTemplateCaches($entry-&gt;id);
  </code></pre>
  
  <p>With that, I didn't need had any of my event listeners in place.</p>
  
  <p>Interesting that things seem to be inserted into craft_templatecachecriteria automatically, but not into craft_templatecacheelements...Perhaps that is bug?</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-09-18T14:45:19.567Z
Answers.LastActivityDate: 2015-09-18T14:45:19.567Z
Answers.UserDisplayName: Tim Kelty
Body: <p>I'm trying to do <code>craft()-&gt;templateCache-&gt;deleteCachesByElementId</code> on <code>entries.onDeleteEntry</code>, unsuccessfully.</p>
  
  <p>The event listener is working, but the caches don't get invalidated.</p>
  
  <p>Here's the code:</p>
  
  <pre><code>public function init()
  {
      craft()-&gt;on('entries.onSaveEntry', function($event) {
          $this-&gt;clearEntryCache($event);
      });
  
      craft()-&gt;on('entries.onDeleteEntry', function($event) {
          $this-&gt;clearEntryCache($event);
      });
  
  }
  
  public function clearEntryCache(Event $event) {
      $entry = $event-&gt;params['entry'];
      craft()-&gt;templateCache-&gt;deleteCachesByElementId($entry-&gt;id, true);
  }
  </code></pre>
  
  <ul>
  <li><code>entries.onSaveEntry</code> works perfectly.</li>
  <li>Instead of run <code>deleteCachesByElementId</code>, if I <code>deleteAllCaches</code>, it works (but I don't want to do this)</li>
  <li>I've also tried <code>entries.onBeforeDeleteEntry</code> with no luck.</li>
  <li>I've tried deleting from the entry, and from the entry listing (bulk delete) screen.</li>
  </ul>
  
  <p>On the surface it seems maybe it isn't working because by the time <code>deleteCachesByElementId</code>, the element is already deleted...though it still seems there should be a way to delete that cache with the elementId reference.</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-09-16T00:59:20.557Z
Comments.Text: Do you have stuck tasks, by chance? (http://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.
Comments.UserDisplayName: Brad Bell
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-09-16T02:06:59.587Z
Comments.Text: No stuck tasks (craft_tasks is empty)
Comments.UserDisplayName: Tim Kelty
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-09-16T02:09:55.040Z
Comments.Text: Just double checked - no errors in logs
Comments.UserDisplayName: Tim Kelty
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-09-16T22:58:58.497Z
Comments.Text: 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.)?
Comments.UserDisplayName: Brad Bell
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-09-18T14:14:34.393Z
Comments.Text: 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.
Comments.UserDisplayName: Tim Kelty
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-09-18T14:19:15.167Z
Comments.Text: Looking into the DB now - I see my cached template in craft_templatecaches, but _nothing_ in craft_templatecachelements. So it seems that's likely the source of my problem?
  
  So perhaps the only reason my saveElement clearing is working is because I'm passing true to the cacheCriteria arg
  
  So I guess I need to find out how to get my elements into the templatecacheelements table
Comments.UserDisplayName: Tim Kelty
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-09-18T14:20:33.920Z
Comments.Text: hmmm as easy as craft()->templateCache->includeElementInTemplateCaches ? Trying now
Comments.UserDisplayName: Tim Kelty
Tags: <plugin-development><cache>
Title: Cannot invalidate template cache (by element) on entries.onDeleteEntry
ThreadText: # Cannot invalidate template cache (by element) on entries.onDeleteEntry
  
  (Asked by: username_0 on 2015-09-15)
  
  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) {
          $this->clearEntryCache($event);
      });
  
      craft()->on('entries.onDeleteEntry', function($event) {
          $this->clearEntryCache($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.
  
  **Tags:** plugin-development, cache
  
  > **Comments:**
  > **username_1**: Do you have stuck tasks, by chance? (http://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.  
  > **username_0**: No stuck tasks (craft_tasks is empty)  
  > **username_0**: Just double checked - no errors in logs  
  > **username_1**: 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.)?  
  > **username_0**: 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.  
  > **username_0**: Looking into the DB now - I see my cached template in craft_templatecaches, but _nothing_ in craft_templatecachelements. So it seems that's likely the source of my problem?  So perhaps the only reason my saveElement clearing is working is because I'm passing true to the cacheCriteria arg  So I guess I need to find out how to get my elements into the templatecacheelements table  
  > **username_0**: hmmm as easy as craft()->templateCache->includeElementInTemplateCaches ? Trying now  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2015-09-18. Score: 1 | Accepted answer
  
  Ok so here's what did it:
  In my plugin, I had to manually call:
  
  ```
          craft()->templateCache->includeElementInTemplateCaches($entry->id);
  
  ```
  
  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?
  
  
  ---
  
  