This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can trysigning in orchanging directories.
Access to this page requires authorization. You can trychanging directories.
Deletes an existing file. This function will fail if any part oflpFileName is redirected via a reparse point or symbolic link.
To perform this operation as a transacted operation, use theDeleteFileTransacted function.
BOOL DeleteFile2A( LPCSTR lpFileName, DWORD Flags);
lpFileName
The name of the file to be deleted.
By default, the name is limited toMAX_PATH characters. To extend this limit to 32,767 wide characters, prepend "\\?\" to the path. For more information, seeNaming Files, Paths, and Namespaces.
Tip
You can opt-in to remove theMAX_PATH limitation without prepending "\\?\". See the "Maximum Path Length Limitation" section ofNaming Files, Paths, and Namespaces for details.
Flags
Flags to specify how to treat the file that is being deleted. This parameter can be a combination one the following values:
Value | Meaning |
---|---|
FILE_FLAGS_DISALLOW_PATH_REDIRECTS0x00000001 | PreventlpFileName from being redirected by reparse points or symbolic links. |
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero (0
). To get extended error information, callGetLastError. Possible errors include the following:
Return code | Description |
---|---|
ERROR_PATH_REDIRECTED | lpFileName was redirected by reparse points and/or symbolic links. |
If an application attempts to delete a file that does not exist, theDeleteFile2 function fails withERROR_FILE_NOT_FOUND. If the file is a read-only file, the function fails withERROR_ACCESS_DENIED.
The following list identifies some tips for deleting, removing, or closing files:
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, seeFile Security and Access Rights.
TheDeleteFile2 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).
TheDeleteFile2 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 toCreateFile,CreateFile2, orCreateFile3 to open the file fail withERROR_ACCESS_DENIED.
The use of POSIX delete causes the file to be deleted while handles remain open. Subsequent calls toCreateFile to open the file fail withERROR_FILE_NOT_FOUND.
If the path points to a symbolic link, the symbolic link is deleted, not the target. To delete a target, you must callCreateFile and specifyFILE_FLAG_DELETE_ON_CLOSE.
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 |
Note
Thefileapi.h
header definesDeleteFile2 as an alias that automatically selects the ANSI or Unicode version of this function based on the definition of the UNICODE preprocessor constant. Mixing usage of the encoding-neutral alias with code that is not encoding-neutral can lead to mismatches that result in compilation or runtime errors. For more information, seeConventions for Function Prototypes.
For an example, seeLocking and Unlocking Byte Ranges in Files.
Requirement | Value |
---|---|
Minimum supported client | Windows 11 24H2 [desktop apps | UWP apps] |
Minimum supported server | Windows Server 2025 [desktop apps | UWP apps] |
Header | fileapi.h (include Windows.h) |
Library | Kernel32.lib |
DLL | Kernel32.dll |
Was this page helpful?
Was this page helpful?