There will be times when you might want to delete the oldest files from a directory using C#. In this tutorial, I have explained different approaches to delete the oldest files in a directory using C#.
To delete the oldest files in a C# directory, use the System.IO namespace to access the directory and retrieve all files. Sort these files by their creation date using LINQ, then delete the specified number of oldest files. This approach ensures efficient file management in C#.
Delete the Oldest Files in a Directory in C#
Here, we will list all the files in a directory in C#, then delete the oldest ones based on their creation date.
Here is a complete C# code:
using System;
using System.IO;
using System.Linq;
public class FileDeleter
{
public static void DeleteOldestFiles(string directoryPath, int filesToDelete)
{
try
{
DirectoryInfo directory = new DirectoryInfo(directoryPath);
FileInfo[] files = directory.GetFiles().OrderBy(f => f.CreationTime).ToArray();
if (files.Length <= filesToDelete) return; // Do not delete if the number of files is less or equal to the threshold
for (int i = 0; i < filesToDelete; i++)
{
files[i].Delete();
Console.WriteLine($"Deleted: {files[i].Name}");
}
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
}
}
class Program
{
static void Main(string[] args)
{
string directoryPath = @"C:\Bijay\DummyFolder"; // Replace with your directory path
int filesToDelete = 3; // Replace with the number of oldest files you want to delete
FileDeleter.DeleteOldestFiles(directoryPath, filesToDelete);
}
}
Code:
- Directory and File Access: We use System.IO namespace to work with files and directories.
- Ordering Files by Creation Date: We retrieve the files and order them by creation time using LINQ.
- Deleting Files: The oldest files (based on the specified number) are deleted, and we handle exceptions to avoid runtime errors.
Here, you can see, in the above folder, I had 5 files like the below screenshot.

After you execute the C# program, you can see it deleted 3 files, which is the number of files to be deleted as mentioned in the program.

Delete Oldest Files in a Directory Using C# (Another approach)
There are multiple ways to delete the oldest files in a directory using C#.
Delete the Oldest files Using DirectoryInfo and FileInfo in C#
In this method, we’ll use classes from the System.IO namespace, specifically DirectoryInfo, and FileInfo, to fetch file information and then delete the oldest files.
First, we need to import the required namespaces in the C# application like below:
using System;
using System.IO;
Then, we can get the files from the directory using the code below:
DirectoryInfo dirInfo = new DirectoryInfo(@"C:\YourDirectory");
FileInfo[] files = dirInfo.GetFiles();
Then, we can sort the files based on date and time.
Array.Sort(files, (f1, f2) => f1.CreationTime.CompareTo(f2.CreationTime));
Finally, we can delete the oldest files by using the below code:
// To delete the oldest file
files[0].Delete();
// To delete the 5 oldest files
for (int i = 0; i < 5; i++)
{
files[i].Delete();
}
Here is the complete code you can run in a console application in C# using Visual Studio.
using System;
using System.IO;
class Program
{
static void Main()
{
DirectoryInfo dirInfo = new DirectoryInfo(@"C:\MyFolder");
FileInfo[] files = dirInfo.GetFiles();
Array.Sort(files, (f1, f2) => f1.CreationTime.CompareTo(f2.CreationTime));
// Delete the oldest file
files[0].Delete();
Console.WriteLine("Oldest file deleted successfully!");
}
}
You can see here I have a folder containing a list of files.

Then, after running the code in a Visual Studio console application, you can see the oldest file has been deleted. See the screenshot below.

Delete the Oldest files in C# using LINQ
LINQ (Language Integrated Query) can make it easier to sort and filter files. Here’s how you can delete the oldest files using LINQ.
For this, we also need first to import the required namespaces.
using System;
using System.IO;
using System.Linq;
Then you can use the below LINQ code to query, sort and delete the oldest files:
var directory = new DirectoryInfo(@"C:\YourDirectory");
var oldestFile = directory.GetFiles()
.OrderBy(f => f.CreationTime)
.First();
oldestFile.Delete();
Here is the example with complete code to delete the oldest files using Linq in C#.
using System;
using System.IO;
using System.Linq;
class Program
{
static void Main()
{
var directory = new DirectoryInfo(@"C:\YourDirectory");
var oldestFile = directory.GetFiles()
.OrderBy(f => f.CreationTime)
.First();
oldestFile.Delete();
Console.WriteLine("Oldest file deleted successfully using LINQ!");
}
}
Conclusion
In this tutorial, we’ve covered two efficient methods to delete the oldest files in a directory using C#. We discussed using DirectoryInfo and FileInfo classes, as well as leveraging LINQ for more streamlined code.
You may also like:
Bijay Kumar is a renowned software engineer, accomplished author, and distinguished Microsoft Most Valuable Professional (MVP) specializing in SharePoint. With a rich professional background spanning over 15 years, Bijay has established himself as an authority in the field of information technology. He possesses unparalleled expertise in multiple programming languages and technologies such as ASP.NET, ASP.NET MVC, C#.NET, and SharePoint, which has enabled him to develop innovative and cutting-edge solutions for clients across the globe. Read more…