Deleting doesn't work. You can only not-write-it-again.
So you read the whole file, close it, overwrite it with the parts you want to keep and don't write the deleted ones again.
Of course, if you want to be safe, first read to another file before deleting the old one, next copy the new one over the old one...