RemoveDirectory function (Windows)

Switch View :
ScriptFree
RemoveDirectory function

Applies to: desktop apps | Metro style apps

Deletes an existing empty directory.

To perform this operation as a transacted operation, use the RemoveDirectoryTransacted function.

Syntax

BOOL WINAPI RemoveDirectory(
  __in  LPCTSTR lpPathName
);

Parameters

lpPathName [in]

The path of the directory to be removed. This path must specify an empty directory, and the calling process must have delete access to the directory.

In the ANSI version of this function, the name is limited to MAX_PATH characters. To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path. For more information, see Naming a File.

Return value

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks

The RemoveDirectory function marks a directory for deletion on close. Therefore, the directory is not removed until the last handle to the directory is closed.

To recursively delete the files in a directory, use the SHFileOperation function.

RemoveDirectory removes a directory junction, even if the contents of the target are not empty; the function removes directory junctions regardless of the state of the target object. For more information on junctions, see Hard Links and Junctions.

Requirements

Minimum supported client

Windows XP

Minimum supported server

Windows Server 2003

Header

WinBase.h (include Windows.h)

Library

Kernel32.lib

DLL

Kernel32.dll

Unicode and ANSI names

RemoveDirectoryW (Unicode) and RemoveDirectoryA (ANSI)

See also

CreateDirectory
Creating and Deleting Directories
Directory Management Functions
RemoveDirectoryTransacted

 

 

Send comments about this topic to Microsoft

Build date: 4/17/2012

Community Content

mhubal
C#
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] public static extern bool RemoveDirectory(string lpPathName);

Thomas Lee
Bypass security
Note: RemoveDirectory will bypass security if SeRestorePrivilege is enabled.

Thomas Lee
bad link
SHFileOperation link is broken