DeleteFileW function (fileapi.h)

The DeleteFileW function deletes an existing file.

Syntax


BOOL DeleteFileW(
  [in] LPCWSTR lpFileName
);
                

Parameters

lpFileName

A pointer to a null-terminated string that names the file to be deleted. For more information, see Remarks.

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 DeleteFileW function can delete files on a local computer or on a remote computer.

To delete a file, you must have delete permission for the file.

If the specified file is read-only, the function fails and returns FALSE.

If the specified file is a hidden file or a system file, the function succeeds.

You can use DeleteFileW to remove an empty directory by specifying the directory name as the lpFileName parameter. However, you cannot delete a directory that contains files or other directories. To delete a directory that is not empty, you must first delete all files and subdirectories within the directory.

If the specified file is open, the DeleteFileW function will fail and return FALSE.

The lpFileName parameter can be a network path.

The following example demonstrates how to delete a file:


#include <windows.h>
#include <iostream>

int main() {
    const WCHAR* filename = L"C:\\Temp\\MyFileToDelete.txt";

    if (DeleteFileW(filename)) {
        std::wcout << L"File '" << filename << L"' deleted successfully." << std::endl;
    } else {
        DWORD error = GetLastError();
        std::wcout << L"Failed to delete file '" << filename << L"'. Error code: " << error << std::endl;
    }

    return 0;
}
            

Requirements

Minimum supported client Windows 2000 Professional
Minimum supported server Windows 2000 Server
Header fileapi.h (include windows.h)
Library Kernel32.lib
DLL Kernel32.dll
Unicode and ANSI names DeleteFileW (Unicode) and DeleteFileA (ANSI)

See also

Note

This is a Microsoft Windows API reference page. Information provided here is specific to the Windows operating system.