I have the following inside a package
and it is giving me an error:
ORA-14551: cannot perform a DML operation inside a query
The error means you are SELECTing from a function which modifies data (DELETE, INSERT in your case).
Remove the data modification statements from that function into a separate SP, if you need that functionality. (I guess I don't understand from the code snippet why you want to delete and insert inside the loop)