How can I programmatically remove the file from a managed_file form element? I have a form where users upload images.  When an image is submitted it is validated against various criteria.  If the validation fails the user is returned to the form but the failed file is still showing in the file upload element with the remove button.

I have been trying to find a way (with no success so far) to automatically remove the file if it fails validation so the user does not have to do it.

It doesn't bother me but the client has requested it so I need to do it somehow.

Any ideas?
hook_field_widget_form_alter(&amp;$element, &amp;$form_state, $context)


this may help you to validate your file and if validation criteria fails return empty form element.