Skip to content

Latest commit

 

History

History
216 lines (163 loc) · 6.43 KB

nf-winbase-deletefile.md

File metadata and controls

216 lines (163 loc) · 6.43 KB
UID title description helpviewer_keywords old-location tech.root ms.assetid ms.date ms.keywords req.header req.include-header req.target-type req.target-min-winverclnt req.target-min-winversvr req.kmdf-ver req.umdf-ver req.ddi-compliance req.unicode-ansi req.idl req.max-support req.namespace req.assembly req.type-library req.lib req.dll req.irql targetos req.typenames req.redist ms.custom f1_keywords dev_langs topic_type api_type api_location api_name
NF:winbase.DeleteFile
DeleteFile function (winbase.h)
The DeleteFile function (winbase.h) deletes an existing file.
DeleteFile
DeleteFile function [Files]
DeleteFileA
DeleteFileW
_win32_deletefile
base.deletefile
fileapi/DeleteFile
fileapi/DeleteFileA
fileapi/DeleteFileW
fs.deletefile
winbase/DeleteFile
winbase/DeleteFileA
winbase/DeleteFileW
fs\deletefile.htm
fs
0b947a85-816b-4374-a8f8-c369e366a17d
08/04/2022
DeleteFile, DeleteFile function [Files], DeleteFileA, DeleteFileW, _win32_deletefile, base.deletefile, fileapi/DeleteFile, fileapi/DeleteFileA, fileapi/DeleteFileW, fs.deletefile, winbase/DeleteFile, winbase/DeleteFileA, winbase/DeleteFileW
winbase.h
Windows.h
Windows
Windows XP [desktop apps \| UWP apps]
Windows Server 2003 [desktop apps \| UWP apps]
DeleteFileW (Unicode) and DeleteFileA (ANSI)
Kernel32.lib
Kernel32.dll
Windows
19H1
DeleteFile
winbase/DeleteFile
c++
APIRef
kbSyntax
DllExport
Kernel32.dll
API-MS-Win-Core-File-l1-1-0.dll
KernelBase.dll
API-MS-Win-Core-File-l1-2-0.dll
API-MS-Win-Core-File-l1-2-1.dll
API-MS-Win-Core-File-l1-2-2.dll
API-MS-Win-DownLevel-Kernel32-l1-1-0.dll
MinKernelBase.dll
DeleteFile
DeleteFileA
DeleteFileW

DeleteFile function

-description

Deletes an existing file.

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

-parameters

-param lpFileName [in]

The name of the file to be deleted.

By default, the name is limited to MAX_PATH characters. To extend this limit to 32,767 wide characters, prepend "\\?\" to the path. For more information, see Naming Files, Paths, and Namespaces.

Tip

Starting with Windows 10, Version 1607, you can opt-in to remove the MAX_PATH limitation without prepending "\\?\". See the "Maximum Path Length Limitation" section of Naming Files, Paths, and Namespaces for details.

-returns

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 has other handles open for normal I/O or as a memory-mapped file (FILE_SHARE_DELETE must have been specified when other handles were opened).

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.

In Windows 8 and Windows Server 2012, this function is supported by the following technologies.

Technology Supported
Server Message Block (SMB) 3.0 protocol Yes
SMB 3.0 Transparent Failover (TFO) Yes
SMB 3.0 with Scale-out File Shares (SO) Yes
Cluster Shared Volume File System (CsvFS) Yes
Resilient File System (ReFS) Yes
 

Examples

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

-see-also

CloseHandle

CreateFile

DeleteFileTransacted

File Management Functions

Symbolic Links