Skip to content

Delete File

A one-line file deletion using DeleteFile, with the CRT unlink alternative commented alongside.

Published Oct 5, 1995

Two ways to delete a file from C on Windows, shown side by side: the Win32 DeleteFile call and the standard-CRT unlink. Both take just a path; DeleteFile returns nonzero on success and sets the last-error code you can inspect with GetLastError if it fails. Use this as the reference whenever your code needs to clean up temporary files at exit.

#include <Windows.h>
#include <Stdio.h>

int main()
{
    DeleteFile("c:/windows/desktop/aaa.bmp");
//    unlink("c:/windows/desktop/aaa.bmp");
    return 0;
}
An unhandled error has occurred. Reload 🗙