How to Delete All Files in a Folder Older Than 7 Days in C#?

There will be times when you will get the requirement to delete all files in a folder older than a specified date or time using C#. In this tutorial, I have explained how to delete all files in a folder older than 7 days in C# using various methods.

Delete All Files in a Folder Older Than 7 Days in C#

Here, I will show you 3 methods to delete all files in a folder older than 7 days in C#.

  • Using System.IO.DirectoryInfo
  • Using System.IO.File
  • Using LINQ

1. Using System.IO.DirectoryInfo

Using the System.IO.DirectoryInfo class, you can easily get all the files in a folder and then filter them based on their last modified date in C#.

Here’s an example that will delete all the files in a folder older than 7 days in C#.

using System;
using System.IO;

class Program
{
    static void Main()
    {
        string folderPath = @"C:\MyFolder";
        DirectoryInfo dirInfo = new DirectoryInfo(folderPath);
        DateTime olderThanDate = DateTime.Now.AddDays(-7);  // Files older than 7 days

        foreach (FileInfo file in dirInfo.GetFiles())
        {
            if (file.LastWriteTime < olderThanDate)
            {
                file.Delete();
            }
        }
    }
}

Once you run the code using Visual Studio, you can see the output below in the screenshot:

c# delete all files in folder older than

2. Using System.IO.File

You can also use the System.IO.File class along with System.IO.Directory to delete all files in a folder older than 7 days in C#.

using System;
using System.IO;

class Program
{
    static void Main()
    {
        string folderPath = @"C:\MyFolder";
        string[] allFiles = Directory.GetFiles(folderPath);
        DateTime olderThanDate = DateTime.Now.AddDays(-7);  // Files older than 7 days

        foreach (string filePath in allFiles)
        {
            DateTime lastModified = File.GetLastWriteTime(filePath);
            if (lastModified < olderThanDate)
            {
                File.Delete(filePath);
            }
        }
    }
}
  • System.IO.Directory.GetFiles method is used to get all the file paths.
  • For each file path, File.GetLastWriteTime gives the last modified time.
  • If the file is older than olderThanDate, it is deleted using File.Delete() method in C#.

3. Using LINQ

You can also use the LINQ code to delete all the files older than 7 days in C#.

Here is the complete code.

using System;
using System.IO;
using System.Linq;

class Program
{
    static void Main()
    {
        string folderPath = @"C:\MyFolder";
        DateTime olderThanDate = DateTime.Now.AddDays(-7);  // Files older than 7 days

        var filesToDelete = new DirectoryInfo(folderPath).GetFiles()
                           .Where(f => f.LastWriteTime < olderThanDate);

        foreach (var file in filesToDelete)
        {
            file.Delete();
        }
    }
}
  • Using LINQ, DirectoryInfo.GetFiles and .Where are chained together to get the files older than olderThanDate.
  • Each file in filesToDelete is then deleted.

Conclusion

You can use System.IO.DirectoryInfo, System.IO.File, or even LINQ to filter and delete files older than a specified time. In this C# tutorial, I have explained with examples how to delete all files in a folder older than 7 days in C#. This way, you can handle “c# delete all files in folder older than“.

You may also like: