In Node, delete all files older than an hour?

node.js delete all files in directory
find-remove npm
how to delete files older than 1 year in linux
script to delete log files older than 30 days
find all files older than 6 months linux
node-red delete file
find files older than 1 day
shell script to delete files older than 7 days

I want to delete any files older than an hour. This is for automatically cleaning up a tmp uploads directory.

Here is my code:

fs.readdir( dirPath, function( err, files ) {
    if ( err ) return console.log( err );
    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 < livesUntil ) {
                fs.unlink( filePath, function( err ) {
                    if ( err ) return console.log( err );

However this just deletes everything in the directory, regardless of whether or not it was uploaded over an hour ago.

Am I misunderstanding how to check the age of the file in Node?

I am using rimraf to recursively delete any file/folder that is older than one hour.

According to the docs, you should use getTime() on the ctime Date object instance in order to compare.

var uploadsDir = __dirname + '/uploads';

fs.readdir(uploadsDir, function(err, files) {
  files.forEach(function(file, index) {
    fs.stat(path.join(uploadsDir, file), function(err, stat) {
      var endTime, now;
      if (err) {
        return console.error(err);
      now = new Date().getTime();
      endTime = new Date(stat.ctime).getTime() + 3600000;
      if (now > endTime) {
        return rimraf(path.join(uploadsDir, file), function(err) {
          if (err) {
            return console.error(err);
          console.log('successfully deleted');

Node.js Recipes, I am using rimraf to recursively delete any file/folder that is older than one hour. According to the docs, you should use getTime() on the ctime� First, let us find out the files older than X days, for example 30 days. To do, so, just run: $ find . -mtime +30 -print. The above command will find and display the older files which are older than 30 day in the current working directorys. Here, dot (.) - Represents the current directory.

I've used find-remove for a similar usecase.

According to the documentation and what you are trying to do, the code will be:

var findRemoveSync = require('find-remove');
findRemoveSync(__dirname + '/uploads', {age: {seconds: 3600}});

I'm actually running that every 6 minutes and deleting files older than 1 hour by doing:

setInterval(findRemoveSync.bind(this,__dirname + '/uploads', {age: {seconds: 3600}}), 360000)

See this example on how I'm using find-remove to cleanup the /uploads folder.

Remove files older than 1 hour � Issue #46 � watson-developer-cloud , The demo runs out of space if we don't remove the files in the upload folder. We used to do this using the find-remove package. The problem in this case is that ForFiles takes a parameter in *days* (/d) and in my case I really want to delete files older than 18 *hours*.

It looks like you're comparing whatever "stat.ctime" is to the entire Date object.

if ( stat.ctime < livesUntil ) {

Shouldn't it read:

if ( stat.ctime < livesUntil.getHours() ) {

Remove files older than 7 days (Example), Sometimes I need to remove files that older than N days (or empty file?). 2 weeks, 1 week, 1 month etc. find DIRECTORY -size 0 \ -or -not� 1. Delete Files Older Than 30 Days. This command will delete all files older than 30 days in system /opt/backup directory. find /opt/backup -type f -mtime +30 -exec rm -f {} \; 2. Delete Files Older Than 30 Days with .log Extension. If you want to delete only specified extension files, you can use the following command.

Simple recursive solution only del all files !dirs run every 5 hours del older files that 1 day

const fs = require('fs');
const path = require('path');

setInterval(function() {
    walkDir('./tmpimages/', function(filePath) {
    fs.stat(filePath, function(err, stat) {
    var now = new Date().getTime();
    var endTime = new Date(stat.mtime).getTime() + 86400000; // 1days in miliseconds

    if (err) { return console.error(err); }

    if (now > endTime) {
        //console.log('DEL:', filePath);
      return fs.unlink(filePath, function(err) {
        if (err) return console.error(err);
}, 18000000); // every 5 hours

function walkDir(dir, callback) {
  fs.readdirSync(dir).forEach( f => {
    let dirPath = path.join(dir, f);
    let isDirectory = fs.statSync(dirPath).isDirectory();
    isDirectory ? 
      walkDir(dirPath, callback) : callback(path.join(dir, f));

Source of function

Delete Files Older Than x Hours on Linux, This can be a path, a directory, or a wildcard as in the example above. The second argument, -mtime, is used to specify the number of days old that the file is . If you� 6. delete all jpg files older than one hour with limit of 100 files deletion per operation var result = findRemoveSync ( ' /tmp ' , {age : {seconds : 3600 }, extensions : ' .jpg ' , limit : 100 }) 7. delete all files with prefix 'filenamestartswith'

find-remove, recursively finds files and/or directories by filter options from a start directory onwards and deletes these you can configure. useful if you want to clean up stuff within a directory in your node.js app. Join in the discussion! �� How to use Task Scheduler to delete files older than X days automatically on Windows 10. The command in the previous instructions allows you to delete files in a folder that are older than 30 days, but you need to open Command Prompt and execute the command manually every time you want to free up space.

在Node中,删除超过一小时的所有文件?, I want to delete any files older than an hour. This is for automatically cleaning up a tmp uploads d. The Ribbon will then look as follows: Here is how to use it to delete files older than a certain number of days. Delete Files Older Than X Days with File Explorer. Open the Search tools tab in the Ribbon (F3). Click on the Date modified button. It has a drop down list with options. Select the desired option, like Last week.

Deleting files older than 1 hours, cache and need me cron script that will delete files older than 1 hour I than 1 hour I have feature in my kloxo control panel just need me the� A simple python script to remove /logs/ files older than 10 days #!/usr/bin/python # run by crontab # removes any files in /logs/ older than 10 days import os, sys, time from subprocess import call def get_file_directory(file): return os.path.dirname(os.path.abspath(file)) now = time.time() cutoff = now - (10 * 86400) files = os.listdir(os.path.join(get_file_directory(__file__), "logs")) file

  • have you tried using .getTime() on both Date objects like the documentation suggests
  • Nitpicky, but in your setInterval example it will execute every 6 minutes (not every hour).
  • Every 6 minutes I'm calling the method to remove files that are older than 1 hour. So each file will live for up to 1 hour and 5 minutes and 59 seconds
  • Right - I'm saying you said "I'm actually running that every hour by doing:" but it's actually running every 6 minutes.
  • Sadly, find-remove is completely synchronous. Do not use in e.g. HTTP requests and with caution.
  • "as a precaution, nothing happens when there are no options." Maybe this was changed in later versions but you also need to specify another option, like files: "*.*", in order for any files to get deleted.
  • stat.ctime is also a date object