How can I delete uploaded files from Gravity Forms submissions while retaining other data?

I’m designing a form that requires users to provide information and attach files, such as images or documents. However, for security reasons, we need to ensure these files are removed from our server after submission, while still keeping the remaining form data for future reference.

Currently, I am using this code, which deletes everything:

// Hook for form ID 5
add_action( 'gform_after_submission_5', 'remove_full_entry' );
function remove_full_entry( $form_id ) {
    // This deletes the entire entry
    GFAPI::delete_entry( $form_id['id'] );
}

While this approach works, it completely erases all submitted information. Is there a way to specifically target and remove only the uploaded files, while preserving text fields and non-file data? I would like to keep the entry available in the admin area without the uploaded files.

You’re deleting the whole entry when you just need to clear the file references. Instead of wiping everything, update the entry fields to remove only the file upload data.

Here’s what worked for me:

add_action( 'gform_after_submission_5', 'remove_uploaded_files_only' );
function remove_uploaded_files_only( $entry, $form ) {
    // Loop through form fields to find file upload fields
    foreach( $form['fields'] as $field ) {
        if( $field->type == 'fileupload' ) {
            // Delete physical files from server
            $file_url = $entry[$field->id];
            if( !empty($file_url) ) {
                $file_path = str_replace( site_url('/'), ABSPATH, $file_url );
                if( file_exists($file_path) ) {
                    unlink($file_path);
                }
                // Clear the field value in the entry
                GFAPI::update_entry_field( $entry['id'], $field->id, '' );
            }
        }
    }
}

This removes the actual files from your server and clears the file field values in the database, but keeps everything else.