There are a couple of ways that this can be done. The first that comes to my mind is to use some sort of cron job that would simply delete old files.
From the cgi script there is not a way to tell if the user doesn't come back (or goes to another page). Plus, I don't think you would want to know this. For instance if they have multiple windows or tabbed browsing, it's quite possible that they would go to another site and then back to your script.