Directory.Delete Directory.Delete Directory.Delete Directory.Delete Method

Definition

Deletes a specified directory, and optionally any subdirectories.

Overloads

Delete(String) Delete(String) Delete(String) Delete(String)

Deletes an empty directory from a specified path.

Delete(String, Boolean) Delete(String, Boolean) Delete(String, Boolean) Delete(String, Boolean)

Deletes the specified directory and, if indicated, any subdirectories and files in the directory.

Delete(String) Delete(String) Delete(String) Delete(String)

Deletes an empty directory from a specified path.

public:
 static void Delete(System::String ^ path);
public static void Delete (string path);
static member Delete : string -> unit
Public Shared Sub Delete (path As String)
Parameters
path
String String String String

The name of the empty directory to remove. This directory must be writable and empty.

Exceptions

A file with the same name and location specified by path exists.

-or-

The directory is the application's current working directory.

-or-

The directory specified by path is not empty.

-or-

The directory is read-only or contains a read-only file.

-or-

The directory is being used by another process.

path is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters by using the GetInvalidPathChars() method.

The specified path, file name, or both exceed the system-defined maximum length.

path does not exist or could not be found.

-or-

The specified path is invalid (for example, it is on an unmapped drive).

Examples

The following example shows how to create a new directory and subdirectory, and then delete only the subdirectory.

using System;
using System.IO;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string subPath = @"C:\NewDirectory\NewSubDirectory";

            try
            {
                Directory.CreateDirectory(subPath);   
                Directory.Delete(subPath);

                bool directoryExists = Directory.Exists(@"C:\NewDirectory");
                bool subDirectoryExists = Directory.Exists(subPath);

                Console.WriteLine("top-level directory exists: " + directoryExists);
                Console.WriteLine("sub-directory exists: " + subDirectoryExists);
            }
            catch (Exception e)
            {
                Console.WriteLine("The process failed: {0}", e.Message);
            }
        }
    }
}
Imports System.IO

Module Module1

    Sub Main()
        Dim subPath = "C:\NewDirectory\NewSubDirectory"

        Try
            Directory.CreateDirectory(subPath)
            Directory.Delete(subPath)

            Dim directoryExists = Directory.Exists("C:\NewDirectory")
            Dim subDirectoryExists = Directory.Exists(subPath)

            Console.WriteLine("top-level directory exists: " & directoryExists)
            Console.WriteLine("sub-directory exists: " & subDirectoryExists)

        Catch e As Exception
            Console.WriteLine("The process failed: {0}", e.Message)
        End Try
    End Sub

End Module

Remarks

This method behaves identically to Delete(String, Boolean) with false specified for the second parameter.

The path parameter may specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see GetCurrentDirectory.

Trailing spaces are removed from the end of the path parameter before deleting the directory.

This method throws an IOException if the directory specified in the path parameter contains files or subdirectories.

The path parameter is not case-sensitive.

In some cases, if you have the specified directory open in File Explorer, the Delete method may not be able to delete it.

Security

FileIOPermission
for writing to the specified directory. Associated enumeration: Write

Delete(String, Boolean) Delete(String, Boolean) Delete(String, Boolean) Delete(String, Boolean)

Deletes the specified directory and, if indicated, any subdirectories and files in the directory.

public:
 static void Delete(System::String ^ path, bool recursive);
public static void Delete (string path, bool recursive);
static member Delete : string * bool -> unit
Public Shared Sub Delete (path As String, recursive As Boolean)
Parameters
path
String String String String

The name of the directory to remove.

recursive
Boolean Boolean Boolean Boolean

true to remove directories, subdirectories, and files in path; otherwise, false.

Exceptions

A file with the same name and location specified by path exists.

-or-

The directory specified by path is read-only, or recursive is false and path is not an empty directory.

-or-

The directory is the application's current working directory.

-or-

The directory contains a read-only file.

-or-

The directory is being used by another process.

path is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters by using the GetInvalidPathChars() method.

The specified path, file name, or both exceed the system-defined maximum length.

path does not exist or could not be found.

-or-

The specified path is invalid (for example, it is on an unmapped drive).

Examples

The following example shows how to create a new directory, subdirectory, and file in the subdirectory, and then recursively delete all the new items.

using System;
using System.IO;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string topPath = @"C:\NewDirectory";
            string subPath = @"C:\NewDirectory\NewSubDirectory";
            
            try
            {
                Directory.CreateDirectory(subPath);

                using (StreamWriter writer = File.CreateText(subPath + @"\example.txt"))
                {
                    writer.WriteLine("content added");
                }
                
                Directory.Delete(topPath, true);

                bool directoryExists = Directory.Exists(topPath);

                Console.WriteLine("top-level directory exists: " + directoryExists);
            }
            catch (Exception e)
            {
                Console.WriteLine("The process failed: {0}", e.Message);
            }
        }
    }
}
Imports System.IO

Module Module1

    Sub Main()
        Dim topPath = "C:\NewDirectory"
        Dim subPath = "C:\NewDirectory\NewSubDirectory"

        Try
            Directory.CreateDirectory(subPath)

            Using writer As StreamWriter = File.CreateText(subPath + "\example.txt")
                writer.WriteLine("content added")
            End Using

            Directory.Delete(topPath, True)

            Dim directoryExists = Directory.Exists(topPath)

            Console.WriteLine("top-level directory exists: " & directoryExists)
        Catch e As Exception
            Console.WriteLine("The process failed: {0}", e.Message)
        End Try
    End Sub

End Module

Remarks

The path parameter may specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see GetCurrentDirectory.

Trailing spaces are removed from the end of the path parameter before deleting the directory.

The path parameter is not case-sensitive.

If the recursive parameter is true, the user must have write permission for the current directory as well as for all subdirectories.

The behavior of this method differs slightly when deleting a directory that contains a reparse point, such as a symbolic link or a mount point. If the reparse point is a directory, such as a mount point, it is unmounted and the mount point is deleted. This method does not recurse through the reparse point. If the reparse point is a symbolic link to a file, the reparse point is deleted and not the target of the symbolic link.

In some cases, if you have the specified directory open in File Explorer, the Delete method may not be able to delete it.

Security

FileIOPermission
for writing to the specified directory. Associated enumeration: Write

Applies to