File save upload doesn't replace existing file despite FILE_EXISTS_REPLACE I'm trying to force replacement of existing file with same name, if a new version is uploaded by user. I've seen a lot of posts about this topic but none that I could find addresses how to force the replacement.

I'm using a custom form, and here's the code I've been trying to make work during the form validation:

  $file = file_save_upload(
      'file', 
      array(
        'file_validate_is_document' =&gt; array(), // Validates file is really a document.
        'file_validate_extensions' =&gt; array('pdf doc ppt pps avi mpg mov wmv'), // Validate extensions.
      ),
      'public://',
      FILE_EXISTS_REPLACE
  );


The revised file (let's say "file.jpg") is uploaded, but inevitably Drupal creates a new copy and renames it file_0.jpg, file_1.jpg, etc for each new version. 

I even tried to programmatically delete the original file using "file_delete" prior to committing the upload but I still get the copy!

Any help would be appreciated.
This is drupal's default behaviour and a known issue. It has been addressed a couple of ways.

The Upload File Replace module addresses this issue by swapping the filenames so the new file retains the original name. 

The Media Update module allows in situ replacement of media files (part of the Media project).

You could either make use of one of these modules, or use them as a model for your own function.