title: Proper NodeJS code to delete old files from a tmp uploads folder
body: <p>I use the following code to delete old files from a tmp upload directory:</p>
  
  <pre><code>       fs.readdir( dirPath, function( err, files ) {
              if ( err ) return console.log( err );
              if (files.length &gt; 0) {
                  files.forEach(function( file ) {
                      var filePath = dirPath + file;
                      fs.stat( filePath, function( err, stat ) {
                          if ( err ) return console.log( err );
                          var livesUntil = new Date();
                          livesUntil.setHours(livesUntil.getHours() - 1);
                          if ( stat.ctime &lt; livesUntil ) {
                              fs.unlink( filePath, function( err ) {
                                  if ( err ) return console.log( err );
                              });
                          }
                      });
                  });
              }
          });
  </code></pre>
  
  <p>I run it once an hour.  This works, most of the time.
  However I am finding that occasionally uploads disappear unexpectedly in the middle of uploading. 
  I <em>think</em> what is happening is that this code gets run while a file is actually in the middle of uploading, before stat.ctime is set - and so it gets deleted before it is finished.
  Any suggestions on how I can prevent this?</p>
  
  <p>Thanks! </p>