Solution
Usually when these errors show up at the Delete file function, a reference is still open to the file you are trying to delete as a result of a previous operation on the file.
An example of the most common case:
Even though the second Open/Create/Replace File function overwrites the original file, the refnum generated by the first Open/Create/Replace File Function is still valid for operations on the file (for example, you can write to the overwritten file using the first refnum - it even retains its original current file position). Until you close all open references (refnums) to a file, you cannot delete that file.
Correct Implementation: