How to clear a physical file (CLRPFM) in a rpgle program when that program is using the file

I have a RPGLE program. It needs to process file FILE1 in input mode after all the data from file is read and processed it needs to clear the file FILE1.

But when I am using QCMDEXE api to clear the file using command CLRPFM, it's throwing an error that The File is in use.

How can I overcome this issue and clear the file at the end of the program. Any suggestions wil really help.


  • I can think of a couple ways. The first and my preference is to use embedded SQL:

    exec sql delete from file;

    This command is smart enough to do a full clear if no one is using the file, but it will just delete all the records if the file is in use, and it can't get the exclusive lock needed to do a clear.

    If SQL is out of the question, you can just delete records using RPG. I would delete each record after it is successfully processed. assuming you are using a READ or READE to read the record, a simple DELETE will do the trick. The advantage of this method is that only successfully processed records are removed, and you can re-run if there are problems that cause a crash for instance. That without worrying about reprocessing records.

    Another thing you might try if you definitely want a CLRPFM is to close the file before doing the clear.