How to delete old images uploaded via FAPI Consider the following bit of code in my custom module... It allows a user to upload an image, saves the fid as a variable, and in a custom submit function, it makes sure that the file in the file_managed table is saved permanently.

This all works great, but when my users are uploading a file, then saving, then removing that old file and uploading another and saving, all of these files are left in the file_managed table, with a status of 1.

function MYMODULE_admin() {
  $form = array();
  $form['style']['business_logo_fid'] = array(
    '#title' =&gt; t('Business Logo'),
    '#type' =&gt; 'managed_file',
    '#description' =&gt; t('Upload a logo for your business in one of the following formats: jpg png jpeg.'),
    '#default_value' =&gt; variable_get('business_logo_fid', ''),
    '#upload_validators' =&gt; array(
    'file_validate_extensions' =&gt; array('jpg png jpeg'),
    ),
    '#upload_location' =&gt; 'public://',
  );

  $form = system_settings_form($form);
  $form['#submit'][] = 'after_save_ui';
  return $form;
}

function after_save_ui($form, &amp;$form_state) {

    $business_logo_fid = variable_get('business_logo_fid');
    if ($business_logo_fid) {
        $business_logo = file_load($business_logo_fid);
        $business_logo-&gt;status = FILE_STATUS_PERMANENT;
        file_save($business_logo);
    }

}


How do i mark these old images as unused after a new one has been saved in its place? Is there something I can do to get the fid from the old file and mark its status as 0 before the new one gets saved? or is this something that Drupal handles already for me?
Its always the way! You spend an hour fussing over it, but when you actually write the question down, you think of it in a different way!

Its a bit long winded, but my complete example is this...

 function MYMODULE_admin() {
      $form = array();
      $form['style']['business_logo_fid'] = array(
        '#title' =&gt; t('Business Logo'),
        '#type' =&gt; 'managed_file',
        '#description' =&gt; t('Upload a logo for your business in one of the following formats: jpg png jpeg.'),
        '#default_value' =&gt; variable_get('business_logo_fid', ''),
        '#upload_validators' =&gt; array(
        'file_validate_extensions' =&gt; array('jpg png jpeg'),
        ),
        '#upload_location' =&gt; 'public://',
      );

      $form = system_settings_form($form);
      $form['#submit'][] = 'after_save_ui';
      return $form;
    }

    function after_save_ui($form, &amp;$form_state) {

        // Save the Business Logo Permenantly.
if (isset($form_state['values']['business_logo_fid'])) {
        $new_logo_id = $form_state['values']['business_logo_fid'];
        $old_logo_id = $form_state['complete form']['style']['business_logo_fid']['#default_value'];
          if (($old_logo_id != 0) &amp;&amp; ($new_logo_id != $old_logo_id)){
            $business_logo_file = file_load($old_logo_id);
            if (!empty($business_logo_file)) {
              file_usage_delete($business_logo_file, 'webfroth_booking_ui', 'business', $business_logo_id);
              file_delete($business_logo_file, TRUE);
              drupal_set_message(t('Old Logo Removed.'));
            }
          }
          if (($new_logo_id != 0) &amp;&amp; ($new_logo_id != $old_logo_id)){
            $current_business_logo = file_load($new_logo_id);
            if (!empty($current_business_logo-&gt;fid)) {
              $business_logo_file_id = $current_business_logo-&gt;fid;
              variable_set('business_logo_fid', $business_logo_file_id);
              file_usage_add($current_business_logo, 'webfroth_booking_ui', 'business', $business_logo_file_id);
              $current_business_logo-&gt;status = FILE_STATUS_PERMANENT;
              file_save($current_business_logo);
              drupal_set_message("New Logo Uploaded Successfully.");
            }
          }
        }
    }


This works great, but it CANT be the standard way to handle this everytime I want to use an image field in a custom module surely? Seems like a massive hassle!