# Expire entire directory of fragments?

**URL:** <https://www.ruby-forum.com/t/expire-entire-directory-of-fragments/155538>\
**Category:** Rails\
**Created:** [December 13, 2008, 4:46am UTC](https://www.ruby-forum.com/t/expire-entire-directory-of-fragments/155538 "2008-12-13T04:46:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![avramd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aca169/32.png) [@avramd](https://www.ruby-forum.com/u/avramd)\
**Post date:** [December 13, 2008, 4:46am UTC](https://www.ruby-forum.com/t/expire-entire-directory-of-fragments/155538/1 "2008-12-13T04:46:17Z")

</div>

Hello,

I am trying to figure out if there is an appropriate and for that  
matter, already built-in way to expire an entire directory of cache  
fragments. My app has certain situations where an entire subdirectory  
in the cache becomes obsolete at once - rather than expiring each file  
one at a time, or using a regexp to iterate over the whole thing, it  
would be nice to just make the whole directory go _poof_.

I realize I could to this manually, but it _feels_ like there is  
something evil about not using expire\_fragment() to get rid of  
something that was made by cache(). For example, I could compute the  
dir manually, and call

system(“rm -rf #{dir}”). Any reason not to do that?

And even slicker solution would be

File::rename(dir, uniq\_filename(‘tmp/old\_caches\_’))

and then let a cron job “rm -rf tmp/old\_caches\_\*” out-of-band.

Can anyone think of a reason not to do this, or in general a good  
reason not to get rid of cache files w/out using expire\_fragment?

Thanks,  
-Avram

So, if there was nobody else when God said “Let there be light,” was  
He talking to Himself? Does that mean He is insane? Because that would  
explain a lot

---

<div class="post-metadata">

**Author:** ![avramd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aca169/32.png) [@avramd](https://www.ruby-forum.com/u/avramd)\
**Post date:** [December 13, 2008, 6:42am UTC](https://www.ruby-forum.com/t/expire-entire-directory-of-fragments/155538/2 "2008-12-13T06:42:37Z")

</div>

> dir manually, and call
> 
> system(“rm -rf #{dir}”). Any reason not to do that?

Don’t make a call to system. Use the File class instead… rm\_rf or  
your rename and then remove depending on how big a directory it is.

If you dig through the cache classes file store eventually calls  
“File.delete(real\_file\_path(name))” so what you’re doing isn’t that  
unreasonable, but it also calls it’s super method and I haven’t bother  
to look to see what it does.

Another option would be to create your own cache store, subclassing  
off of FileStore and then override the delete method to check if it’s  
a directory being passed in and if so, wipe the whole thing out,  
otherwise just the file. Or look to see if there is a reason not to  
just do that directly in FileStore and submit a ticket to have it  
patched…
