Deletes a file.
Note This function works with wide characters. Use RemoveFile for an ANSI version of this function.
BOOL RemoveFileW(
LPCWSTR lpFileName
);
| Parameter | Description |
|---|---|
lpFileName |
A pointer to a null-terminated string that names the file to be deleted. This parameter can specify a relative path or an absolute path. |
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.
The RemoveFileW function can delete a file that is open for reading but not for writing. If the file is open for writing, the function will fail and return zero. The calling process must have delete access to the file.
If the specified file is a directory, the function fails and returns zero. To delete a directory, use the RemoveDirectory function.
The RemoveFileW function may not work as expected for remote files, because the exact behavior depends on the network redirector and the server's implementation.
The RemoveFileW function is unaffected by symbolic links. If lpFileName is a symbolic link to a file, the function will attempt to delete the symbolic link itself, not the target file.
For a more detailed explanation of file operations and error handling, refer to the File Management Concepts and Error Handling in Windows documentation.
The following example demonstrates how to use the RemoveFileW function to delete a file:
// #include <windows.h>
// #include <stdio.h>
int main() {
LPCWSTR fileName = L"my_test_file.txt";
// Create a dummy file to delete
HANDLE hFile = CreateFileW(
fileName,
GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL
);
if (hFile == INVALID_HANDLE_VALUE) {
DWORD error = GetLastError();
printf("Failed to create file. Error: %lu\n", error);
return 1;
}
CloseHandle(hFile);
printf("Created dummy file: %ws\n", fileName);
// Attempt to delete the file
if (RemoveFileW(fileName)) {
printf("Successfully deleted file: %ws\n", fileName);
} else {
DWORD error = GetLastError();
printf("Failed to delete file: %ws. Error: %lu\n", fileName, error);
}
return 0;
}
| Attribute | Value |
|---|---|
| Minimum supported client | Windows 2000 Professional |
| Minimum supported server | Windows 2000 Server |
| Product | Windows |
| Header | fileapi.h (include windows.h) |
| Library | Kernel32.lib |
| DLL | Kernel32.dll |
| Unicode character set | Implemented on Unicode and ANSI versions. |