Connecting Tech Pros Worldwide Forums | Help | Site Map

Deleting all tmp files(session,cookies,cache) in ruby

Newbie
 
Join Date: Oct 2007
Posts: 19
#1: Jul 7 '08
Hello,
For my ruby on rails application,i need to delete all the temporary files created in /tmp folder.If those files increases, at some point in time, my application stops working unless i delete all session files manually.Is there any way, so that i can keep on deleting those files from the folder automatically or somehow.Please help if anyone knows.Its urgent.

Thanks in advance

acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#2: Jul 7 '08

re: Deleting all tmp files(session,cookies,cache) in ruby


If you know the name of the file, just use File.delete("nameoffile.ext") For more information on input/output, see this tutorial.
Newbie
 
Join Date: Oct 2007
Posts: 19
#3: Jul 8 '08

re: Deleting all tmp files(session,cookies,cache) in ruby


Quote:

Originally Posted by acoder

If you know the name of the file, just use File.delete("nameoffile.ext") For more information on input/output, see this tutorial.

No.I think i was not clear about my question.In tmp/session folder,
ruby_sess.7e406b1b7791bd09
ruby_sess.10645ae490aa2726..
This kind of many files get created.I want clear all the files regularly.I should delete all the files in that folder.How can i do this?Hope i am clear this time.

Thanks
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#4: Jul 8 '08

re: Deleting all tmp files(session,cookies,cache) in ruby


Use the FileUtils module. Loop over the directory. If it's a file, use rm and if it's a directory use rm_f to delete the files/directories (or you could use rm with option force set to true).

Make an attempt at that and see what you can come up with.
Expert
 
Join Date: May 2007
Posts: 213
#5: Jul 8 '08

re: Deleting all tmp files(session,cookies,cache) in ruby


I have no experience with FileUtils, but that may work exactly as you need. My only question about it is, will it delete current sessions? Is that a concern? If so, you could try using Dir to get all files in the tmp directory, then loop over them. Then for each file, you can check its last modified time (using File.stat) to determine if you want to delete it. I've never used this for the tmp directory, but it works for other situations.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#6: Jul 8 '08

re: Deleting all tmp files(session,cookies,cache) in ruby


Good point. Always a good idea to look at the wider picture.
Newbie
 
Join Date: Aug 2008
Posts: 2
#7: Aug 2 '08

re: Deleting all tmp files(session,cookies,cache) in ruby


Hello,

Good day ! :)

I am also in a same position as you.I need to delete all my session files from /tmp directory.Did you found any alternative for that.

Please help me in this issue.I would be Great to you if you can help me in resolving this.

Cheers,
Rajesh
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#8: Aug 2 '08

re: Deleting all tmp files(session,cookies,cache) in ruby


Did you try the suggestions above?
Newbie
 
Join Date: Aug 2008
Posts: 2
#9: Aug 5 '08

re: Deleting all tmp files(session,cookies,cache) in ruby


Yes I tried a bit But No idea at all.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#10: Aug 5 '08

re: Deleting all tmp files(session,cookies,cache) in ruby


What did you try? Both suggestions should work. Is it the looping part that's causing you problems. Show your code.
Reply