<p>This is a side-effect of Craft's amazing automagical cache system. :) Check out my answer on <a href="https://craftcms.stackexchange.com/questions/15924/delete-stale-template-caches-tasks-much-larger-in-production-than-staging">this question</a> for an explanation of what happens. </p>

<p>Regarding your question of how to fix this, part of the solution is probably what I wrote in the answer, to make sure that you don't create more caches than you need, by caching globally and using the request path as key.</p>

<p>Other ways to counter this problem is to use the <a href="https://github.com/pixelandtonic/ColdCache" rel="nofollow noreferrer">ColdCache</a> and <a href="https://github.com/mmikkel/CacheFlag-Craft" rel="nofollow noreferrer">CacheFlag</a> plugins. They serve different purposes, and isn't right for all caches, but they have one thing in common, they doesn't save anything to the <code>templatecachecriteria</code> table, which determines the length of the delete stale template caches task. I usually end up with a combination of all three on projects that have a lot of content.</p>

<p>ColdCache is good to use in the cases where you primarily are caching a single entry, and you can get away with breaking the cache when the entry is saved. By using the modified date of the entry as part of your cache key, Craft will create a new cache every time you save the entry (don't worry, the old caches will be deleted when they expire, and doesn't affect performance in any way).</p>

<p>CacheFlag is good to use when you know what sections, globals, categories or... whatever, you're caching. For instance, if you have a news section, and are caching a list of news entries, you could add a flag to the section in CacheFlags settings, wrap the list in the CacheFlag tags and add the flag. Whenever an entry in that section gets saved, all caches with that flag gets deleted and refreshed the next time the list is shown.</p>

<p>Both of these approaches sacrifices something in terms of cache breaking - and it takes some thinking through to get right - but performance wise you gain a lot.</p>

CC BY-SA 3.0
2016-10-06T15:29:54.943Z
Thanks for your detailed answer. I will check out the plugins you mentioned.
Bart Rylant
CC BY-SA 3.0
2016-10-06T14:11:03.977Z
2016-10-06T14:11:03.977Z
2017-04-13T12:51:00.200Z
André Elvan
<p>I have a website with a few thousand entries. At the bottom of the entry template, we show 4 random news posts from the last 4 weeks.</p>

<p>Now, when someone adds a new entry, the <code>DeleteStaleTemplateCaches</code> task starts running. This causes cpu and memory peaks on our server that go to 100%. This task also takes about 30 minutes to complete, meaning that our cached homepage only shows the new article after 30 minutes.</p>

<p>Any ideas why it takes so long, solutions on how to fix this? (Apart from caching the 4 entries on the bottom of the page, and caching the homepage, we didn't do anything weird.)</p>

<cache>
DeleteStaleTemplateCaches taking forever
# DeleteStaleTemplateCaches taking forever

(Asked by: username_0 on 2016-10-06)

I have a website with a few thousand entries. At the bottom of the entry template, we show 4 random news posts from the last 4 weeks.

Now, when someone adds a new entry, the `DeleteStaleTemplateCaches` task starts running. This causes cpu and memory peaks on our server that go to 100%. This task also takes about 30 minutes to complete, meaning that our cached homepage only shows the new article after 30 minutes.

Any ideas why it takes so long, solutions on how to fix this? (Apart from caching the 4 entries on the bottom of the page, and caching the homepage, we didn't do anything weird.)

**Tags:** cache

---

## 1 answers

### Answer by username_1 on 2016-10-06. Score: 6 | Accepted answer

This is a side-effect of Craft's amazing automagical cache system. :) Check out my answer on [this question](https://craftcms.stackexchange.com/questions/15924/delete-stale-template-caches-tasks-much-larger-in-production-than-staging) for an explanation of what happens.

Regarding your question of how to fix this, part of the solution is probably what I wrote in the answer, to make sure that you don't create more caches than you need, by caching globally and using the request path as key.

Other ways to counter this problem is to use the [ColdCache](https://github.com/pixelandtonic/ColdCache) and [CacheFlag](https://github.com/mmikkel/CacheFlag-Craft) plugins. They serve different purposes, and isn't right for all caches, but they have one thing in common, they doesn't save anything to the `templatecachecriteria` table, which determines the length of the delete stale template caches task. I usually end up with a combination of all three on projects that have a lot of content.

ColdCache is good to use in the cases where you primarily are caching a single entry, and you can get away with breaking the cache when the entry is saved. By using the modified date of the entry as part of your cache key, Craft will create a new cache every time you save the entry (don't worry, the old caches will be deleted when they expire, and doesn't affect performance in any way).

CacheFlag is good to use when you know what sections, globals, categories or... whatever, you're caching. For instance, if you have a news section, and are caching a list of news entries, you could add a flag to the section in CacheFlags settings, wrap the list in the CacheFlag tags and add the flag. Whenever an entry in that section gets saved, all caches with that flag gets deleted and refreshed the next time the list is shown.

Both of these approaches sacrifices something in terms of cache breaking - and it takes some thinking through to get right - but performance wise you gain a lot.

> **Comments:**
> **username_0**: Thanks for your detailed answer. I will check out the plugins you mentioned.  

---

