RemoveFileW function (File API)

Deletes a file.

Note This function works with wide characters. Use RemoveFile for an ANSI version of this function.

Syntax

BOOL RemoveFileW(
              LPCWSTR lpFileName
            );

Parameters

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.

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 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.

Example

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;
}

Explanation of the Example

  1. The example first includes necessary headers: <windows.h> for Win32 API functions and <stdio.h> for standard input/output (like printf).
  2. A wide character string literal L"my_test_file.txt" is defined to hold the filename.
  3. CreateFileW is used to create a simple file named my_test_file.txt. This is done so that there is a file to actually delete.
  4. Error handling is included for CreateFileW. If the file creation fails, an error message is printed, and the program exits.
  5. The handle to the created file is closed using CloseHandle.
  6. The core functionality: RemoveFileW is called with the filename to delete the created file.
  7. The return value of RemoveFileW is checked. A success message is printed if the file is deleted, otherwise an error message (including the specific Windows error code from GetLastError) is displayed.

Requirements

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.

See also