C program to delete a file - GeeksforGeeks (2023)

Improve Article

Save Article

  • Difficulty Level :Easy
  • Last Updated :21 Nov, 2017

Improve Article

Save Article

The remove function in C/C++ can be used to delete a file. The function returns 0 if files is deleted successfully, other returns a non-zero value.

(Video) Linux Tutorials | rm - Removing a file | GeeksforGeeks


int main()


if (remove("abc.txt") == 0)

(Video) Converting one string to other using append and delete last operations | GeeksforGeeks

printf("Deleted successfully");


printf("Unable to delete the file");

return 0;


Using remove() function in C, we can write a program which can destroy itself after it is compiled and executed.

Explanation: This can be done using the remove function in C. Note that, this is done in Linux environment. So, the remove function is fed the first parameter in command line argument i.e. a.out file (executable file) created after compiling . Hence the program will be destroyed.

(Video) Linux Tutorials | cp - Copying a File | GeeksforGeeks



int main(int c, char *argv[])


printf("By the time you will compile me I will be destroyed \n");

// array of pointers to command line arguments


(Video) How to deallocate memory without using free() in C? | GeeksforGeeks

// Note: argv[0] will contain the executable file i.e. 'a.out'

return 0;


// This code is contributed by MAZHAR IMAM KHAN.


By the time you will compile me I will be destroyed

After the output shown above, the a.out file will be removed.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

My Personal Notesarrow_drop_up

(Video) Linux Tutorials | mv-Renaming and moving files | GeeksforGeeks


1. C++ Program to remove spaces from a string | GeeksforGeeks
2. Remove all characters other than alphabets in a string. | GeeksforGeeks
3. Linked List | Set 3 (Deleting a node) | GeeksforGeeks
4. Linux Tutorials | Command Usage | GeeksforGeeks
5. Preprocessor & Predefined directives, File Functions, command line arguments, function pointers |GFG
(GeeksforGeeks-Campus Connect)
6. Memory layout of C programs | GeeksforGeeks
Top Articles
Latest Posts
Article information

Author: Rev. Leonie Wyman

Last Updated: 12/03/2022

Views: 5826

Rating: 4.9 / 5 (59 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Rev. Leonie Wyman

Birthday: 1993-07-01

Address: Suite 763 6272 Lang Bypass, New Xochitlport, VT 72704-3308

Phone: +22014484519944

Job: Banking Officer

Hobby: Sailing, Gaming, Basketball, Calligraphy, Mycology, Astronomy, Juggling

Introduction: My name is Rev. Leonie Wyman, I am a colorful, tasty, splendid, fair, witty, gorgeous, splendid person who loves writing and wants to share my knowledge and understanding with you.