Home » Programming Languages » C Programs » C program to delete a file – remove API

C program to delete a file – remove API

In this simple C program, we demonstrate how to use Remove System call from C to delete the file created in current working directory. You can modify, extend the code as needed.

$ vim remove_delete_file.c
#include <stdio.h>

int main(int argc, char **argv) {
	int r;
	const char *filename = "./testfile";
	r = remove(filename);
	if (r == 0)
		printf("file : %s removed successfully\n", filename);
	else
		perror(filename);

	return 0;
}
$ gcc -o remove_delete_file remove_delete_file.c

Create a test file,

$ echo "This is testfile" > testfile

Now, we try to delete this same file we created as above,

$ ./remove_delete_file

Once we executed the binary created by compiling C program, we can see that the test file we created gets deleted.

Leave a Comment