Answers.Body: <p>This sounds like a bug, and I think I know why. Can you try changing the <code>{% set entryCount ...</code> line to this?</p>
  
  <pre><code>{% set entryCount = craft.entries.relatedTo(category.id).total() %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-03T07:58:55.807Z
Answers.Text: Sure, thanks. I get the same behaviour. Adding works, removing not.
Answers.UserDisplayName: Pascal Mollet
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-28T09:46:27.547Z
Answers.LastActivityDate: 2014-10-28T09:46:27.547Z
Answers.UserDisplayName: Brandon Kelly
Body: <p>I'm currently working on integration some caching behaviour using <a href="http://buildwithcraft.com/docs/templating/cache" rel="nofollow"><code>{% cache %}</code></a> tags. This works mostly just fine, but I have some issues when I work with related entries.</p>
  
  <p>For example I have a list of categories with an indicator to show many entries they are assigned to.</p>
  
  <ul>
  <li>New (3)</li>
  <li>Used (2)</li>
  <li>Broken (3)</li>
  </ul>
  
  <p>If I change an entry and add a category, the cache refreshes and shows the correct amount. But if I remove a category from an entry I get the wrong number until I refresh the cache manually.</p>
  
  <p>Is this the expected behaviour? Or do I miss something here?</p>
  
  <p>Here is my code example:</p>
  
  <pre><code>{% cache unless not craft.config.cache %}
  {% set categorySlug = productSection ~ 'Categories' %}
  
  &lt;ul&gt;
  
  {% for category in craft.categories.group(categorySlug) %}
    {% set entryCount = craft.entries.relatedTo(category).total() %}
    {% if entryCount &gt; 0 %}
  
      &lt;li&gt;
        {{ category.title }} ( {{ entryCount }} )
      &lt;/li&gt;
  
    {% endif %}
  {% endfor %}
  
  &lt;/ul&gt;
  
  {% endcache %}
  </code></pre>
  
Tags: <templating><relations><cache>
Title: Caching and related Entries
ThreadText: # Caching and related Entries
  
  (Asked by: username_0 on 2014-10-24)
  
  I'm currently working on integration some caching behaviour using [`{% cache %}`](http://buildwithcraft.com/docs/templating/cache) tags. This works mostly just fine, but I have some issues when I work with related entries.
  
  For example I have a list of categories with an indicator to show many entries they are assigned to.
  
  * New (3)
  * Used (2)
  * Broken (3)
  
  If I change an entry and add a category, the cache refreshes and shows the correct amount. But if I remove a category from an entry I get the wrong number until I refresh the cache manually.
  
  Is this the expected behaviour? Or do I miss something here?
  
  Here is my code example:
  
  ```
  {% cache unless not craft.config.cache %}
  {% set categorySlug = productSection ~ 'Categories' %}
  
  <ul>
  
  {% for category in craft.categories.group(categorySlug) %}
    {% set entryCount = craft.entries.relatedTo(category).total() %}
    {% if entryCount > 0 %}
  
      <li>
        {{ category.title }} ( {{ entryCount }} )
      </li>
  
    {% endif %}
  {% endfor %}
  
  </ul>
  
  {% endcache %}
  
  ```
  
  **Tags:** templating, relations, cache
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2014-10-28. Score: 1
  
  This sounds like a bug, and I think I know why. Can you try changing the `{% set entryCount ...` line to this?
  
  ```
  {% set entryCount = craft.entries.relatedTo(category.id).total() %}
  
  ```
  
  > **Comments:**
  > **username_0**: Sure, thanks. I get the same behaviour. Adding works, removing not.  
  
  ---
  
  