I am working with files in a Drupal module. I added this form:

$form['Background_image'] = [
  '#type'               => 'managed_file',
  '#title'              => t('Image'),
  '#progress_message'   => t('Please wait...'),
  '#progress_indicator' => 'bar',
  '#description'        => t('Click "Browse..." to select an image to upload.'),
  '#required'           => TRUE,
  '#upload_validators'  => ['file_validate_extensions' => ['jpeg jpg png gif']],
  '#upload_location'    => 'public://backgroundimage/',
  '#default_value'      => $this->options['Background_image'],

The file is added properly. Once the file is uploaded a remove button appears allowing to remove the file and upload a new one. The problem is that this button is not working. How can I remove an uploaded file?


Once you have uploaded a file you must be having file object so use file_delete(stdClass $file, $force = FALSE) to delete the file.

| improve this answer | |
  • Normally that have to be done by the file module no? – LearningHowToBeAGoodProgrammer Oct 6 '13 at 0:24
  • yes on cron run it happens, but isn't this you aksed for? – arpitr Oct 6 '13 at 0:29
  • yes but though once the button remove appears it have to work without adding a extra code, like once the form managed_file is created ,the function upload and remove are called automaticly once the user click the button.. – LearningHowToBeAGoodProgrammer Oct 6 '13 at 0:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.