File Management Functions


DeleteFile Function

Deletes an existing file.

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

Syntax

C++
BOOL WINAPI DeleteFile(
  __in  LPCTSTR lpFileName
);

Parameters

lpFileName [in]

The name of the file to be deleted.

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 (0). To get extended error information, call GetLastError.

Remarks

If an application attempts to delete a file that does not exist, the DeleteFile function fails with ERROR_FILE_NOT_FOUND. If the file is a read-only file, the function fails with ERROR_ACCESS_DENIED.

The following list identifies some tips for deleting, removing, or closing files:

  • To delete a read-only file, first you must remove the read-only attribute.
  • To delete or rename a file, you must have either delete permission on the file, or delete child permission in the parent directory.
  • To recursively delete the files in a directory, use the SHFileOperation function.
  • To remove an empty directory, use the RemoveDirectory function.
  • To close an open file, use the CloseHandle function.

If you set up a directory with all access except delete and delete child, and the access control lists (ACL) of new files are inherited, then you can create a file without being able to delete it. However, then you can create a file, and then get all the access you request on the handle that is returned to you at the time you create the file.

If you request delete permission at the time you create a file, you can delete or rename the file with that handle, but not with any other handle. For more information, see File Security and Access Rights.

The DeleteFile function fails if an application attempts to delete a file that is open for normal I/O or as a memory-mapped file.

The DeleteFile function marks a file for deletion on close. Therefore, the file deletion does not occur until the last handle to the file is closed. Subsequent calls to CreateFile to open the file fail with ERROR_ACCESS_DENIED.

Symbolic link behavior—

If the path points to a symbolic link, the symbolic link is deleted, not the target. To delete a target, you must call CreateFile and specify FILE_FLAG_DELETE_ON_CLOSE.

Examples

For an example, see Locking and Unlocking Byte Ranges in Files.

Requirements

Minimum supported clientWindows 2000 Professional
Minimum supported serverWindows 2000 Server
HeaderWinBase.h (include Windows.h)
LibraryKernel32.lib
DLLKernel32.dll
Unicode and ANSI namesDeleteFileW (Unicode) and DeleteFileA (ANSI)

See Also

CloseHandle
CreateFile
DeleteFileTransacted
File Management Functions
Symbolic Links

Send comments about this topic to Microsoft

Build date: 11/12/2009

Tags :


Community Content

Gideon7
Bypass security
Note: DeleteFile will bypass security if SeRestorePrivilege is enabled.
Tags : deletefile

dmex
vb.net syntax
<DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
Public Shared Function DeleteFile(ByVal path As String) As Boolean
End Function
Tags : vb.net syntax

dmex
C# syntax
[DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
public static extern bool DeleteFile(string path);
Tags : c# syntax

Thomas Lee
Alternative to unmanaged code

As an alternative to calling into this unmanaged api, you might consider using the System.IO.File class and its DeleteFileMethod.

For more information on the class, see http://msdn.microsoft.com/en-us/library/system.io.file(VS.80).aspx and for more information on the DeleteFile method, see: http://msdn.microsoft.com/en-us/library/system.io.file.delete(VS.80).aspx.


Page view tracker