Tuesday, December 1, 2009

APEX Orphaned Application Files

If you allow end users to upload files to your APEX application you may have a lot of "orphaned" files in apex_application_files and not even realize it.

Orphaned files are files that exist in APEX_APPLICATION_FILES that are not associated with an application. This can happen for several reasons, the most common are:

  • Files uploaded in Shared Components that aren't associated to an application

  • End users uploading files then purposely keep them in APEX_APPLICATION_FILES

  • End users uploading files and an error occurs



You can easily identify orphaned files using the APEX_APPLICATION_FILES view:

SELECT *
FROM apex_application_files
WHERE flow_id = 0 -- flow_id is the same as application_id

All 3 situations listed above will result in the file uploaded with flow_id = 0. The last 2 points, files uploaded from end users, can result in files that you may no longer need. I don't recommend that you keep uploaded files in the APEX_APPLICATION_FILES view. Instead you should move them immediately to a custom table.

The main problem comes from the third point. When a user uploads a file and a validation fails. In this situation the file is uploaded to APEX_APPLICATION_FILES and then the validation fails. Even though the validation failed, the file still resides in APEX_APPLICATION_FILES. The following screen shot demonstrates this issue.


To resolve this issue, I run the following application process which automatically "tags" uploaded files with a flow_id of -1. By doing so you can run a nightly process to delete any files that have a flow_id of -1.

Application Process: AP_TAG_APEX_FILES
Sequence: -100
Point: On Submit: After Page Submission - Before Computations and Validations

-- AP_TAG_APEX_FILES
BEGIN
FOR x IN (SELECT v (item_name) item_name_value
FROM apex_application_page_items
WHERE application_id = :app_id
AND page_id = :app_page_id
AND display_as = 'File Browse...')
LOOP
UPDATE apex_application_files aaf
SET aaf.flow_id = -1
WHERE aaf.NAME = x.item_name_value;
END LOOP;
END;