in reply to deleting a file after loading

One possibility is to delete old files at the beginning of the script. When your script is executed by the web server, it could first delete all its images files that are older than 5 minutes, and then create a new image file. The five minute period is important in case your script is run by more than one user at the same time. Here's a quick example:
my $time = time; my $image_dir = '/path/to/script\'s/private/image/dir'; opendir(DIR, $image_dir) or die "Can't opendir $image_dir: $!\n"; while (defined(my $filename = readdir(DIR))) { next unless -f "$image_dir/$filename"; next unless time - (stat(_))[9] > 300; unlink("$image_dir/$filename") or warn "Can't unlink $image_dir/$filename: $!\n"; }