Defined in header <stdio.h>
int remove( const char *fname );

Deletes the file identified by character string pointed to by fname.

If the file is currently open by this or another process, the behavior of this function is implementation-defined (in particular, POSIX systems unlink the file name although the file system space is not reclaimed until the last running process closes the file; Windows does not allow the file to be deleted).


fname - pointer to a null-terminated string containing the path identifying the file to delete

Return value

​0​ upon success or non-zero value on error.


POSIX specifies many additional details for the behavior of this function.


#include <stdio.h>
int main(void)
    FILE* fp = fopen("file1.txt", "w"); // create file
    if(!fp) { perror("file1.txt"); return 1; }
    puts("Created file1.txt");
    int rc = remove("file1.txt");
    if(rc) { perror("remove"); return 1; }
    puts("Removed file1.txt");
    fp = fopen("file1.txt", "r"); // Failure: file does not exist
    if(!fp) perror("Opening removed file failed");
    rc = remove("file1.txt"); // Failure: file does not exist
    if(rc) perror("Double-remove failed");


Created file1.txt
Removed file1.txt
Opening removed file failed: No such file or directory
Double-remove failed: No such file or directory


  • C11 standard (ISO/IEC 9899:2011):
    • The remove function (p: 302)
  • C99 standard (ISO/IEC 9899:1999):
    • The remove function (p: 268)
  • C89/C90 standard (ISO/IEC 9899:1990):
    • The remove function

See also

renames a file
C++ documentation for remove

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.