Cleanup Temp Files

I’ve been spring-cleaning some devices and the obviousness of the advice in the title seems questionable. I found tons of unused temp files which applications (android apps, desktop applications and even server application deployments) haven’t cleaned. This is taking up space and it means more manual maintenance.

For server-side applications the impact is probably smaller, as it is entirely under your control and you can regularly cleanup the data, or even don’t care, as you regularly re-create the machine (e.g. in an AWS deployment where each upgrade to the system means new machines get spawned and the old ones – deleted). But anyway, if you use temp files (and Java), use File.createTempFile(..) and don’t forget to call file.deleteOnExit().

For client-side applications (smartphone apps or desktop software) the carelessness of not deleting temp files leads to the users’ disappointment at some point in time, when they realize their storage is filled with your useless files. The delete-on-exist works again, but maybe you need the files to survive more than one run. So simply have a job, or a startup-check that checks whether temp files aren’t older than a certain period, and if they are – delete them.

The effect of this little thing being omitted by developers is that users have to analyze their storage with special tools (sometimes – paid) in order to find the “offenders”. And the offenders are not always obvious, and besides – users are not necessarily familiar with the concept of a temp file. Even I’m sometimes not sure of a given file that looks like a temp one isn’t actually necessary for the proper functioning.

Storage is cheap, but good practices should not be abandoned because of that.

2 thoughts on “Cleanup Temp Files”

  1. I believe the proper name of the method is deleteOnExit(), sans the ‘s’. Other than that, good article.

  2. I think it might be good to mention that, actually, temp files are bad smell by themselves.
    Why you gonna need this? Why not to use memory or streams?
    Yes, it not always easy to do, but it’s more better and powerful.
    I used temporary files only once in life – when I needed to call another old app that accepts only archived with rar files.

    BTW, you can create a in-memory disk and mount temp folder and after restart everything will be clear

Leave a Reply

Your email address will not be published.