DeleteDirectory Method (My.Computer.FileSystem Object)
Collapse the table of content
Expand the table of content
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer

My.Computer.FileSystem.DeleteDirectory Method 

Deletes a directory.

' Usage
My.Computer.FileSystem.DeleteDirectory(directory ,onDirectoryNotEmpty)
My.Computer.FileSystem.DeleteDirectory(directory ,showUI ,recycle)
My.Computer.FileSystem.DeleteDirectory(directory ,showUI ,recycle ,onUserCancel)
' Declaration
Public Sub DeleteDirectory( _
   ByVal directory As String, _
   ByVal onDirectoryNotEmpty As DeleteDirectoryOption _
' -or-
Public Sub DeleteDirectory( _
   ByVal directory As String, _
   ByVal showUI As UIOption, _
   ByVal recycle As RecycleOption _
' -or-
Public Sub DeleteDirectory( _
   ByVal directory As String, _
   ByVal showUI As UIOption, _
   ByVal recycle As RecycleOption, _
   ByVal onUserCancel As UICancelOption _



String. Directory to be deleted. Required.


DeleteDirectoryOption Enumeration. Specifies what should be done when a directory that is to be deleted contains files or directories. Default is DeleteDirectoryOption.DeleteAllContents.


UIOption Enumeration. Specifies whether to visually track the operation's progress. Default is UIOption.OnlyErrorDialogs. Required.


RecycleOption Enumeration. Specifies whether or not the deleted file should be sent to the Recycle Bin. Default is RecycleOption.DeletePermanently.


UICancelOption Enumeration. Specifies whether to throw an exception if the user clicks Cancel. Required.

The following conditions may cause an exception:

  • The path is a zero-length string, is malformed, contains only white space, or contains invalid characters (including wildcard characters) (ArgumentException).

  • The path is a device path (starts with \\.\) (ArgumentException).

  • The path is Nothing (ArgumentNullException).

  • The directory does not exist or is a file (DirectoryNotFoundException).

  • The directory is not empty, and onDirectoryNotEmpty is set to ThrowIfDirectoryNonEmpty (IOException).

  • The user does not have permission to delete the directory or subdirectory (IOException).

  • A file in the directory or subdirectory is in use (IOException).

  • A file or directory name contains a colon (:) (NotSupportedException).

  • The user cancels the operation or the directory cannot be deleted (OperationCanceledException).

  • The path exceeds the system-defined maximum length (PathTooLongException).

  • The user does not have required permissions (SecurityException).

The showUI, recycle, and onUserCancel parameters are not supported in applications that are not user interactive, such as Windows Services.

The following table lists an example of a task involving the My.Computer.FileSystem.DeleteDirectory method.

To See

Delete a directory

How to: Delete a Directory in Visual Basic

The example deletes the directory OldDirectory only if it is empty.

My.Computer.FileSystem.DeleteDirectory _
("C:\OldDirectory", FileIO.DeleteDirectoryOption.ThrowIfDirectoryNonEmpty)

This example deletes the directory OldDirectory and all of its contents.

My.Computer.FileSystem.DeleteDirectory _
("C:\OldDirectory", FileIO.DeleteDirectoryOption.DeleteAllContents)

This example deletes the directory OldDirectory and all of its contents, asking the user to confirm the deletion, but does not send the contents to the Recycle Bin.

My.Computer.FileSystem.DeleteDirectory _
("C:\OldDirectory", FileIO.UIOption.AllDialogs, FileIO.RecycleOption.DeletePermanently, FileIO.UICancelOption.ThrowException)

This example deletes the directory OldDirectory and all of its contents, sending them to the Recycle Bin, but does not show the progress of the operation.

My.Computer.FileSystem.DeleteDirectory("C:\OldDirectory", _
 FileIO.UIOption.AllDialogs, FileIO.RecycleOption.SendToRecycleBin, FileIO.UICancelOption.ThrowException)

Namespace: Microsoft.VisualBasic.MyServices

Class: FileSystemProxy (provides access to FileSystem)

Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)

Availability by Project Type

Project type Available

Windows Application


Class Library


Console Application


Windows Control Library


Web Control Library


Windows Service


Web Site


The following permissions may be necessary:

Permission Description


Controls the ability to access files and folders. Associated enumeration: Unrestricted.


Controls the permissions related to user interfaces and the clipboard. Associated enumeration: SafeSubWindows.

For more information, see Code Access Security and Requesting Permissions.

© 2015 Microsoft