How do you handle cleaning up old files?

I’m finding that old versions of the JDK, Tomcat installations, etc accumulate over time and eat up quite a bit of disk space. My solution was going to be a new attribute that is essentially just an array of paths, and a small recipe that loops through these and calls the directory resource to do the delete. Wasn’t sure if there was a more elegant solution out there I should consider. Thanks!


The approach usually taken is to have the server treated more as cattle, so
destroy and rebuild.
Whilst you might not want to do this every time you upgrade tomcat when you
first start. It is in the long term much easier to deal with (no loops to
come in and check files are deleted etc).


I mean I agree with that sentiment in general, any server should be replaceable at any time if it disappears for whatever reason. It seems like overkill to never do an in place upgrade though. Especially for internal servers like nexus, jenkins, etc.

Found a related ticket on the java cookbook:

The best time to clean up something like that is usually when the next version is being installed. That’s actually one of the reasons I have a policy of not installing anything from source whenever possible. I make an exception in Docker containers because those are throw-away, you just rebuild a new one from scratch with the new software version.

Your list of paths is certainly another option, although that can become very difficult to manage over time.

Yet another option applies to situations where there is a consistent pattern to the directory naming, and where only one recipe is responsible for maintaining that. In that case, you can build your list of paths automatically - you know what should be in there, and you can assume that everything else is crud (do be aware that sometimes people put stuff there manually - but the idea behind Chef is that Chef trumps anything manual!)

For instance, if your JDK installations always follow a pattern such as this:


Then you can use Ruby code to list all directories under /opt/jdk. Look for the one version that should be installed and remove it from that list. Then use a directory resource with the remove action on each.

This Stack Overflow question has a lot of options on how to enumerate directories:

Kevin Keane
The NetTech
Our values: Privacy, Liberty, Justice