45

How can I find out when a file was created using java, as I wish to delete files older than a certain time period, currently I am deleting all files in a directory, but this is not ideal:

public void DeleteFiles() {
    File file = new File("D:/Documents/NetBeansProjects/printing~subversion/fileupload/web/resources/pdf/");
    System.out.println("Called deleteFiles");
    DeleteFiles(file);
    File file2 = new File("D:/Documents/NetBeansProjects/printing~subversion/fileupload/Uploaded/");
    DeleteFilesNonPdf(file2);
}

public void DeleteFiles(File file) {
    System.out.println("Now will search folders and delete files,");
    if (file.isDirectory()) {
        for (File f : file.listFiles()) {
            DeleteFiles(f);
        }
    } else {
        file.delete();
    }
}

Above is my current code, I am trying now to add an if statement in that will only delete files older than say a week.

EDIT:

@ViewScoped
@ManagedBean
public class Delete {

    public void DeleteFiles() {
        File file = new File("D:/Documents/NetBeansProjects/printing~subversion/fileupload/web/resources/pdf/");
        System.out.println("Called deleteFiles");
        DeleteFiles(file);
        File file2 = new File("D:/Documents/NetBeansProjects/printing~subversion/fileupload/Uploaded/");
        DeleteFilesNonPdf(file2);
    }

    public void DeleteFiles(File file) {
        System.out.println("Now will search folders and delete files,");
        if (file.isDirectory()) {
            System.out.println("Date Modified : " + file.lastModified());
            for (File f : file.listFiles()) {
                DeleteFiles(f);
            }
        } else {
            file.delete();
        }
    }

Adding a loop now.

EDIT

I have noticed while testing the code above I get the last modified in :

INFO: Date Modified : 1361635382096

How should I code the if loop to say if it is older than 7 days delete it when it is in the above format?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user2065929
  • 1,065
  • 4
  • 21
  • 35

15 Answers15

50

You can use File.lastModified() to get the last modified time of a file/directory.

Can be used like this:

long diff = new Date().getTime() - file.lastModified();

if (diff > x * 24 * 60 * 60 * 1000) {
    file.delete();
}

Which deletes files older than x (an int) days.

33

Commons IO has built-in support for filtering files by age with its AgeFileFilter. Your DeleteFiles could just look like this:

import java.io.File;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.AgeFileFilter;
import static org.apache.commons.io.filefilter.TrueFileFilter.TRUE;

// a Date defined somewhere for the cutoff date
Date thresholdDate = <the oldest age you want to keep>;

public void DeleteFiles(File file) {
    Iterator<File> filesToDelete =
        FileUtils.iterateFiles(file, new AgeFileFilter(thresholdDate), TRUE);
    for (File aFile : filesToDelete) {
        aFile.delete();
    }
}

Update: To use the value as given in your edit, define the thresholdDate as:

Date tresholdDate = new Date(1361635382096L);
schnatterer
  • 7,525
  • 7
  • 61
  • 80
Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
  • 1
    Is there any way you can combine the AgeFileFilter with another filter, such as the NameFileFilter? – NathanChristie Apr 03 '15 at 17:56
  • @NathanChristie see [AndFileFilter](http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/filefilter/AndFileFilter.html) and [OrFileFilter](http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/filefilter/OrFileFilter.html) – maxb3k Jun 22 '15 at 22:35
  • Parameters: iterateFiles(File directory, IOFileFilter fileFilter, IOFileFilter dirFilter), dirfilter is optional (can be null) – thijsraets Jan 12 '16 at 14:44
  • 1
    Example does not compile, cannot iterate over `Iterator` Probably want to use listFiles? – NickL Feb 04 '22 at 10:52
15

Using Apache utils is probably the easiest. Here is the simplest solution I could come up with.

public void deleteOldFiles() {
    Date oldestAllowedFileDate = DateUtils.addDays(new Date(), -3); //minus days from current date
    File targetDir = new File("C:\\TEMP\\archive\\");
    Iterator<File> filesToDelete = FileUtils.iterateFiles(targetDir, new AgeFileFilter(oldestAllowedFileDate), null);
    //if deleting subdirs, replace null above with TrueFileFilter.INSTANCE
    while (filesToDelete.hasNext()) {
        FileUtils.deleteQuietly(filesToDelete.next());
    }  //I don't want an exception if a file is not deleted. Otherwise use filesToDelete.next().delete() in a try/catch
}
MattC
  • 5,874
  • 1
  • 47
  • 40
  • 2
    It's also worth noting that you can use milliseconds to create an AgeFileFilter like this `new AgeFileFilter(System.currentTimeMillis() - AGE_LIMIT_MILLIS)` where AGE_LIMIT_MILLIS could be say 24*60*60*1000L for 24 hours. – Glenn Lawrence Dec 28 '16 at 09:07
  • @MattC Does this have any impact like out of memory exception if i use it for the directory having 2 or 3 million records? – diwa Apr 07 '17 at 06:33
  • @Diwa Yes, I am guessing you could run into memory issues if you had millions of files. FileUtils creates a java.util.LinkedList, and then returns the iterator of that list. – MattC Apr 07 '17 at 15:59
14

Example using Java 8's Time API

LocalDate today = LocalDate.now();
LocalDate eailer = today.minusDays(30);
    
Date threshold = Date.from(eailer.atStartOfDay(ZoneId.systemDefault()).toInstant());
AgeFileFilter filter = new AgeFileFilter(threshold);
    
File path = new File("...");
File[] oldFolders = FileFilterUtils.filter(filter, path);
    
for (File folder : oldFolders) {
    System.out.println(folder);
}
Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • 3
    To be clear, AgeFileFilter is from Apache Commons IO, not from the Java 8 Time API. – Nate Dec 15 '19 at 12:31
12

Using lambdas (Java 8+)

Non recursive option to delete all files in current folder that are older than N days (ignores sub folders):

public static void deleteFilesOlderThanNDays(int days, String dirPath) throws IOException {
    long cutOff = System.currentTimeMillis() - (days * 24 * 60 * 60 * 1000);
    Files.list(Paths.get(dirPath))
    .filter(path -> {
        try {
            return Files.isRegularFile(path) && Files.getLastModifiedTime(path).to(TimeUnit.MILLISECONDS) < cutOff;
        } catch (IOException ex) {
            // log here and move on
            return false;
        }
    })
    .forEach(path -> {
        try {
            Files.delete(path);
        } catch (IOException ex) {
            // log here and move on
        }
    });
}

Recursive option, that traverses sub-folders and deletes all files that are older than N days:

public static void recursiveDeleteFilesOlderThanNDays(int days, String dirPath) throws IOException {
    long cutOff = System.currentTimeMillis() - (days * 24 * 60 * 60 * 1000);
    Files.list(Paths.get(dirPath))
    .forEach(path -> {
        if (Files.isDirectory(path)) {
            try {
                recursiveDeleteFilesOlderThanNDays(days, path.toString());
            } catch (IOException e) {
                // log here and move on
            }
        } else {
            try {
                if (Files.getLastModifiedTime(path).to(TimeUnit.MILLISECONDS) < cutOff) {
                    Files.delete(path);
                }
            } catch (IOException ex) {
                // log here and move on
            }
        }
    });
}
rouble
  • 16,364
  • 16
  • 107
  • 102
12

Here's Java 8 version using Time API. It's been tested and used in our project:

    public static int deleteFiles(final Path destination,
        final Integer daysToKeep) throws IOException {

    final Instant retentionFilePeriod = ZonedDateTime.now()
            .minusDays(daysToKeep).toInstant();

    final AtomicInteger countDeletedFiles = new AtomicInteger();
    Files.find(destination, 1,
            (path, basicFileAttrs) -> basicFileAttrs.lastModifiedTime()
                    .toInstant().isBefore(retentionFilePeriod))
            .forEach(fileToDelete -> {
                try {
                    if (!Files.isDirectory(fileToDelete)) {
                        Files.delete(fileToDelete);
                        countDeletedFiles.incrementAndGet();
                    }
                } catch (IOException e) {
                    throw new UncheckedIOException(e);
                }
            });

    return countDeletedFiles.get();
}
Erikson
  • 549
  • 1
  • 6
  • 16
  • I was adapting this for what I needed, and had a warning from SonarQube noting that the Files.find stream should be closed, or ideally put into a try with resources block. – Matt Harrison Nov 17 '20 at 22:03
6

For a JDK 8 solution using both NIO file streams and JSR-310

long cut = LocalDateTime.now().minusWeeks(1).toEpochSecond(ZoneOffset.UTC);
Path path = Paths.get("/path/to/delete");
Files.list(path)
        .filter(n -> {
            try {
                return Files.getLastModifiedTime(n)
                        .to(TimeUnit.SECONDS) < cut;
            } catch (IOException ex) {
                //handle exception
                return false;
            }
        })
        .forEach(n -> {
            try {
                Files.delete(n);
            } catch (IOException ex) {
                //handle exception
            }
        });

The sucky thing here is the need for handling exceptions within each lambda. It would have been great for the API to have UncheckedIOException overloads for each IO method. With helpers to do this one could write:

public static void main(String[] args) throws IOException {
    long cut = LocalDateTime.now().minusWeeks(1).toEpochSecond(ZoneOffset.UTC);
    Path path = Paths.get("/path/to/delete");
    Files.list(path)
            .filter(n -> Files2.getLastModifiedTimeUnchecked(n)
                    .to(TimeUnit.SECONDS) < cut)
            .forEach(n -> {
                System.out.println(n);
                Files2.delete(n, (t, u)
                              -> System.err.format("Couldn't delete %s%n",
                                                   t, u.getMessage())
                );
            });
}


private static final class Files2 {

    public static FileTime getLastModifiedTimeUnchecked(Path path,
            LinkOption... options)
            throws UncheckedIOException {
        try {
            return Files.getLastModifiedTime(path, options);
        } catch (IOException ex) {
            throw new UncheckedIOException(ex);
        }
    }

    public static void delete(Path path, BiConsumer<Path, Exception> e) {
        try {
            Files.delete(path);
        } catch (IOException ex) {
            e.accept(path, ex);
        }
    }

}
Brett Ryan
  • 26,937
  • 30
  • 128
  • 163
6

JavaSE Canonical Solution. Delete files older than expirationPeriod days.

private void cleanUpOldFiles(String folderPath, int expirationPeriod) {
    File targetDir = new File(folderPath);
    if (!targetDir.exists()) {
        throw new RuntimeException(String.format("Log files directory '%s' " +
                "does not exist in the environment", folderPath));
    }

    File[] files = targetDir.listFiles();
    for (File file : files) {
        long diff = new Date().getTime() - file.lastModified();

        // Granularity = DAYS;
        long desiredLifespan = TimeUnit.DAYS.toMillis(expirationPeriod); 

        if (diff > desiredLifespan) {
            file.delete();
        }
    }
}

e.g. - to removed all files older than 30 days in folder "/sftp/logs" call:

cleanUpOldFiles("/sftp/logs", 30);

Alexander Drobyshevsky
  • 3,907
  • 2
  • 20
  • 17
4

You can get the creation date of the file using NIO, following is the way:

BasicFileAttributes attrs = Files.readAttributes(file, BasicFileAttributes.class);
System.out.println("creationTime: " + attrs.creationTime());

More about it can be found here : http://docs.oracle.com/javase/tutorial/essential/io/fileAttr.html

vikasing
  • 11,562
  • 3
  • 25
  • 25
  • `BasicFileAttributes` is available only with java 7. You can not use it with java 6 or earlier releases. –  Feb 23 '13 at 16:53
3

Another approach with Apache commons-io and joda:

private void deleteOldFiles(String dir, int daysToRemainFiles) {
    Collection<File> filesToDelete = FileUtils.listFiles(new File(dir),
            new AgeFileFilter(DateTime.now().withTimeAtStartOfDay().minusDays(daysToRemainFiles).toDate()),
            TrueFileFilter.TRUE);    // include sub dirs
    for (File file : filesToDelete) {
        boolean success = FileUtils.deleteQuietly(file);
        if (!success) {
            // log...
        }
    }
}
Isaace
  • 131
  • 6
3

Using Java NIO Files with lambdas & Commons IO

final long time = System.currentTimeMillis();
// Only show files & directories older than 2 days
final long maxdiff = TimeUnit.DAYS.toMillis(2);

List all found files and directories:

Files.newDirectoryStream(Paths.get("."), p -> (time - p.toFile().lastModified()) < maxdiff)
.forEach(System.out::println);

Or delete found files with FileUtils:

Files.newDirectoryStream(Paths.get("."), p -> (time - p.toFile().lastModified()) < maxdiff)
.forEach(p -> FileUtils.deleteQuietly(p.toFile()));
socona
  • 432
  • 3
  • 13
1

Here is the code to delete files which are not modified since six months & also create the log file.

package deleteFiles;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

public class Delete {
    public static void deleteFiles()
    {
        int numOfMonths = -6;
        String path="G:\\Files";
        File file = new File(path);
        FileHandler fh;
        Calendar sixMonthAgo = Calendar.getInstance();
        Calendar currentDate = Calendar.getInstance();
        Logger logger = Logger.getLogger("MyLog");
        sixMonthAgo.add(Calendar.MONTH, numOfMonths);
        File[] files = file.listFiles();
        ArrayList<String> arrlist = new ArrayList<String>();

        try {
            fh = new FileHandler("G:\\Files\\logFile\\MyLogForDeletedFile.log");
            logger.addHandler(fh);
            SimpleFormatter formatter = new SimpleFormatter();
            fh.setFormatter(formatter);

            for (File f:files)
            {
                if (f.isFile() && f.exists())
                {
                    Date lastModDate = new Date(f.lastModified());
                    if(lastModDate.before(sixMonthAgo.getTime()))
                    {
                        arrlist.add(f.getName());
                        f.delete();
                    }
                }
            }
            for(int i=0;i<arrlist.size();i++)
                logger.info("deleted files are ===>"+arrlist.get(i));
        }
        catch ( Exception e ){
            e.printStackTrace();
            logger.info("error is-->"+e);
        }
    }
    public static void main(String[] args)
    {
        deleteFiles();
    }
}
Pankaj Dagar
  • 87
  • 10
  • It's more helpful to explain the parts that the OP doesn't understand, rather than providing them with a bulk of code that does approximately what they wanted. Take a look at the accepted answer here, it has much less code than yours, but more explanation and it solved the question succinctly. – SuperBiasedMan Aug 11 '16 at 10:36
0

Need to point out a bug on the first solution listed, x * 24 * 60 * 60 * 1000 will max out int value if x is big. So need to cast it to long value

long diff = new Date().getTime() - file.lastModified();

if (diff > (long) x * 24 * 60 * 60 * 1000) {
    file.delete();
}
Feng Zhang
  • 1,698
  • 1
  • 17
  • 20
0

Perhaps this Java 11 & Spring solution will be useful to someone:

private void removeOldBackupFolders(Path folder, String name) throws IOException {
    var current = System.currentTimeMillis();
    var difference = TimeUnit.DAYS.toMillis(7);

    BiPredicate<Path, BasicFileAttributes> predicate =
        (path, attributes) ->
            path.getFileName().toString().contains(name)
                && (current - attributes.lastModifiedTime().toMillis()) > difference;

    try (var stream = Files.find(folder, 1, predicate)) {
      stream.forEach(
          path -> {
            try {
              FileSystemUtils.deleteRecursively(path);

              log.warn("Deleted old backup {}", path.getFileName());
            } catch (IOException lambdaEx) {
              log.error("", lambdaEx);
            }
          });
    }
}

The BiPredicate is used to filter files (i.e. files & folder in Java) by name and age.

FileSystemUtils.deleteRecursively() is a Spring method that recursively removes files & folders. You can change that to something like NIO.2 Files.files.walkFileTree() if you don't want to use Spring dependencies.

I've set the maxDepth of Files.find() to 1 based on my use case. You can set to it unlimited Integer.MAX_VALUE and risk irreversibly deleting your dev FS if you are not careful.

Example logs based on var difference = TimeUnit.MINUTES.toMillis(3):

2022-05-20 00:54:15.505  WARN 24680 --- [       single-1] u.t.s.service.impl.BackupServiceImpl     : Deleted old backup backup_20052022_1652989557462
2022-05-20 00:54:15.506  WARN 24680 --- [       single-1] u.t.s.service.impl.BackupServiceImpl     : Deleted old backup backup_20052022_1652989558474
2022-05-20 00:54:15.507  WARN 24680 --- [       single-1] u.t.s.service.impl.BackupServiceImpl     : Deleted old backup backup_20052022_1652989589723
2022-05-20 00:54:15.508  WARN 24680 --- [       single-1] u.t.s.service.impl.BackupServiceImpl     : Deleted old backup backup_20052022_1652989674083

Notes:

  • The stream of Files.find() must be wrapped inside of a try-with-resource (utilizing AutoCloseable) or handled the old-school way inside of a try-finally to close the stream.

  • A good example of Files.walkFileTree() for copying (can be adapted for deletion): https://stackoverflow.com/a/60621544/3242022

downvoteit
  • 588
  • 2
  • 7
  • 12
-1

Using Apache commons-io and joda:

        if ( FileUtils.isFileOlder(f, DateTime.now().minusDays(30).toDate()) ) {
            f.delete();
        }