# Call expire\_fragment from outside sweepers or controllers

**URL:** <https://www.ruby-forum.com/t/call-expire-fragment-from-outside-sweepers-or-controllers/145884>\
**Category:** Rails\
**Created:** [August 14, 2008, 7:23pm UTC](https://www.ruby-forum.com/t/call-expire-fragment-from-outside-sweepers-or-controllers/145884 "2008-08-14T19:23:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![xponrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/df705f/32.png) [@xponrails](https://www.ruby-forum.com/u/xponrails)\
**Post date:** [August 14, 2008, 7:23pm UTC](https://www.ruby-forum.com/t/call-expire-fragment-from-outside-sweepers-or-controllers/145884/1 "2008-08-14T19:23:43Z")

</div>

Hi all.

I need to expire cached fragments from outside sweepers or  
controllers: I have a rake task that calls a method inside a module in  
the /lib directory. This task will be called at a scheduled time by a  
crontab job.

After method execution I would like to expire some cached fragments.

I’ve just tried to import ActionController::Caching and  
ActionController::Caching::Fragment, but without success.

Do you have any suggestions?

---

<div class="post-metadata">

**Author:** ![xponrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/df705f/32.png) [@xponrails](https://www.ruby-forum.com/u/xponrails)\
**Post date:** [September 8, 2010, 3:18pm UTC](https://www.ruby-forum.com/t/call-expire-fragment-from-outside-sweepers-or-controllers/145884/2 "2010-09-08T15:18:10Z")

</div>

xponrails wrote:

> Hi all.
> 
> I need to expire cached fragments from outside sweepers or  
> controllers: I have a rake task that calls a method inside a module in  
> the /lib directory. This task will be called at a scheduled time by a  
> crontab job.
> 
> After method execution I would like to expire some cached fragments.
> 
> I’ve just tried to import ActionController::Caching and  
> ActionController::Caching::Fragment, but without success.
> 
> Do you have any suggestions?

I had exactly the same problem and managed to come up with a solution  
(albeit with a couple of limitations). My solution involves keeping all  
the code in the Sweeper and calling it via a class method, so it should  
work anywhere. See my blog entry for an explanation and the code.

[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)
