Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

How to loop through folder to delete files and subfolders in ASP.NET

Author: Rachel Nicole

You need to do recursion to delete files and subfolders from a specific folder. Code provided in this article will help you to do this task easily.


You cannot delete a directory if it is not empty. So you should have to delete files and subdirectories to delete the directory. A file cannot be deleted if it has ReadOnly permission.

To delete files and subfolders in a folder, you have to do it recursively. Recursion provides extremely powerful solution to a problem in a finite number of steps that have infinite number of computations. As in case of directories, subdirectories and files in directories. In Programming Language, a function calls itself within its code is recursion.
Sometimes you need to delete files that are older than a month or a year. You can do it easily in ASP.NET. A code snippet is provided below. Import "System.IO" namespace to use this code.
using System.IO;
private void DeleteFilesAndFolders() {
        DirectoryInfo dirInfo = new DirectoryInfo(Server.MapPath("~/NewFolder1"));
        DirectoryInfo[] subDirs = dirInfo.GetDirectories();
        foreach (DirectoryInfo dir in subDirs) {
    private void FindAndDeleteFolder(DirectoryInfo drinfo) {
        DirectoryInfo[] subDirs = drinfo.GetDirectories();
        foreach (DirectoryInfo dir in subDirs) {
        if (drinfo.GetFiles().Length == 0) {
    private void DeleteFilesInFolder(DirectoryInfo di)
        FileInfo[] fileInfo = di.GetFiles();
        foreach (FileInfo file in fileInfo) {
            if(file.CreationTime < DateTime.Now.AddMonths(-1)) {
Imports System.IO
Private Sub DeleteFilesAndFolders()
        Dim dirInfo As DirectoryInfo = New DirectoryInfo(Server.MapPath("~/NewFolder1"))
        Dim subDirs As DirectoryInfo() = dirInfo.GetDirectories()
        For Each dir As DirectoryInfo In subDirs
    End Sub
    Private Sub FindAndDeleteFolder(ByVal drinfo As DirectoryInfo)
        Dim subDirs As DirectoryInfo() = drinfo.GetDirectories()
        For Each dir As DirectoryInfo In subDirs
        If drinfo.GetFiles().Length = 0 Then
        End If
    End Sub
    Private Sub DeleteFilesInFolder(ByVal di As DirectoryInfo)
        Dim fileInfo As FileInfo() = di.GetFiles()
        For Each File As FileInfo In fileInfo
            If File.CreationTime < DateTime.Now.AddMonths(-1) Then
            End If
    End Sub
There are three functions in above code. In first function DeleteFilesAndFolder(), I have created a DirectoryInfo object and provided the path of the directory. Server.MapPath(“~/NewFolder1”) means that I have a folder named “NewFolder1” at the root of my application. Then I have called the GetDirectories() method of the DirectoryInfo object. This method returns the array of type DirectoryInfo. Then I iterated through each Directory and called the method FindAndDeleteFolder(). After that I have called the DeleteFilesInFolder() method.
FindAndDeleteFolder() method takes the DirectoryInfo object as parameter. I recursively called the FindAndDeleteFolder() in it then called the DeleteFilesInFolder() method. At the end of this method, I have deleted this directory if it is empty.
DeleteFilesInFolder() method takes the DirectoryInfo object as parameter. GetFiles() method of DirectoryInfo object returns the array of type FileInfo. At the end I iterate through all the files and call the Delete() method of FileInfo object if the file is older than 1 month. Providing value in minus in AddMonth() or AddYear() methods mean that it will subtract mentioned months or years. You can also use Subtract() method to subtract DateTime or TimeSpan Value. 
You can also delete files that has specific extension by mentioning the Search Option in GetFiles() method.


Add Article Comment:
Name :
Email Address :
Comments :
<< Data Caching in ASP.NET

Disclaimer - Privacy
© 2002-2017