# Expire cache in rake issue

**URL:** <https://www.ruby-forum.com/t/expire-cache-in-rake-issue/121873>\
**Category:** Rails\
**Created:** [December 2, 2007, 7:14am UTC](https://www.ruby-forum.com/t/expire-cache-in-rake-issue/121873 "2007-12-02T07:14:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![vinay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/6a8cbe/32.png) [@vinay](https://www.ruby-forum.com/u/vinay)\
**Post date:** [December 2, 2007, 7:14am UTC](https://www.ruby-forum.com/t/expire-cache-in-rake-issue/121873/1 "2007-12-02T07:14:46Z")

</div>

Hi All,  
I am trying to expire a fragment cache in rake. I know that the best  
practice to expire the cache is in the Sweepers. But as I have to expire  
the cache at the end of the month I was thinking on the lines of doing  
it in a rake task.  
I had tried this but it showed me an error while running the task

expire\_fragment(:controller =\> :login, :action =\> :index, :something=\>  
“1”)

Well it gives me a “No method Error”

Wanted to know what is the best practice to clear the cache on a cron  
job? Should a sweeper action be called or a rake task is prescribed?

If any one has done this would highly appreciate if he/she could provide  
the code for calling a sweeper action from a cron or can someone point  
out what I am missing in the rake file. Do I have to create an object  
and then call the method if so then an object of what class?

Please pardon me for being such a newbie. Any help would be highly  
appreciated.

Cheers,  
Jazzy

---

<div class="post-metadata">

**Author:** ![vinay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/6a8cbe/32.png) [@vinay](https://www.ruby-forum.com/u/vinay)\
**Post date:** [January 3, 2008, 10:01am UTC](https://www.ruby-forum.com/t/expire-cache-in-rake-issue/121873/2 "2008-01-03T10:01:52Z")

</div>

Any body knows how to get this done?

---

<div class="post-metadata">

**Author:** ![vinay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/6a8cbe/32.png) [@vinay](https://www.ruby-forum.com/u/vinay)\
**Post date:** [May 4, 2008, 9:42pm UTC](https://www.ruby-forum.com/t/expire-cache-in-rake-issue/121873/3 "2008-05-04T21:42:46Z")

</div>

Hate to bring back an old topic but I just came across this in my  
frantic search to find a solution to much the same problem. So here it  
goes:

In you rake task call: ApplicationController.expire\_page(…)

---

<div class="post-metadata">

**Author:** ![vinay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/6a8cbe/32.png) [@vinay](https://www.ruby-forum.com/u/vinay)\
**Post date:** [May 4, 2008, 9:44pm UTC](https://www.ruby-forum.com/t/expire-cache-in-rake-issue/121873/4 "2008-05-04T21:44:12Z")

</div>

Silviu lucian Vacaroiu wrote:

> Hate to bring back an old topic but I just came across this in my  
> frantic search to find a solution to much the same problem. So here it  
> goes:
> 
> In you rake task call: ApplicationController.expire\_page(…)

of course, make sure your environment gets setup first:

task :expire\_cache =\> :environment do  
ApplicationController.expire\_page(:controller =\> “welcome”)  
end

---

<div class="post-metadata">

**Author:** ![vinay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/6a8cbe/32.png) [@vinay](https://www.ruby-forum.com/u/vinay)\
**Post date:** [May 4, 2008, 10:10pm UTC](https://www.ruby-forum.com/t/expire-cache-in-rake-issue/121873/5 "2008-05-04T22:10:05Z")

</div>

no, sorry, scratch that

I was trying it in the console and though it was working there I realize  
now it’s not the same thing. In the end I got away with using something  
like:

task :expire\_cache =\> :environment do  
ActionController::Base::expire\_page(’/’)  
end

In my case, I wanted to delete ‘index.html’ (root page-cache)

---

<div class="post-metadata">

**Author:** ![vinay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/6a8cbe/32.png) [@vinay](https://www.ruby-forum.com/u/vinay)\
**Post date:** [September 8, 2010, 3:02pm UTC](https://www.ruby-forum.com/t/expire-cache-in-rake-issue/121873/6 "2010-09-08T15:02:14Z")

</div>

I struggled with this problem the other night and managed to come up  
with solution that allowed me to to use the code from my Sweeper instead  
of duplicating it in rake. I wrote a blog entry explaining what I did  
along with code samples.

[http://stevencummings.name/2010/09/07/expire\_fragment-with-rake-and-as-a-model-only-observer](http://stevencummings.name/2010/09/07/expire_fragment-with-rake-and-as-a-model-only-observer)

---

<div class="post-metadata">

**Author:** ![vinay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/6a8cbe/32.png) [@vinay](https://www.ruby-forum.com/u/vinay)\
**Post date:** [September 8, 2010, 6:05pm UTC](https://www.ruby-forum.com/t/expire-cache-in-rake-issue/121873/7 "2010-09-08T18:05:36Z")

</div>

jazzy jazzy wrote:

> Hi All,  
> I am trying to expire a fragment cache in rake. I know that the best  
> practice to expire the cache is in the Sweepers. But as I have to expire  
> the cache at the end of the month I was thinking on the lines of doing  
> it in a rake task.  
> I had tried this but it showed me an error while running the task

You could look into Rails.cache.delete(path\_and\_fragment\_name)
