Remove an uploaded file I am working with files in a Drupal module. I added this form:

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