Removing files is pretty easy. Use the unlink() system call:
int unlink (const char *path);
unlink() returns zero on successful completion or -1 if there was an error, with errno set to the error that actually happened. There can be multiple references to a file via hardlinks. unlink() removes a reference from a particular dictionary, but the file’s contents are only removed when all references to it have been eliminated.
There is one subtlety when unlinking files that are currently open. The directory entry for the file is removed immediately. If you do an ls on the directory, you won’t see the file. But the file’s contents still exist on the disk, still consume space, and are still available to programs that have the file open. ...