How can I stop auto uploading of managed_file elements? I am trying to create a field to upload the xml format of site map. But when I add a file to my filed it automatically uploaded and saved in the S3 bucket before I click on the submit button.

The issue is when someone added a xml file to that field and then closes the tab without submitting the form. The xml file is already uploaded to S3 bucket that way.

I need a way to manually upload this. I need to upload the image only after I pressed the submit button in the form. Without uploading the xml file automatically when selecting.

This is my buildForm() function.

public function buildForm(array $form, FormStateInterface $form_state) {

  $form['sitemap'] = [
    '#type'              =&gt; 'managed_file',
    '#name'              =&gt; 'sitemap',
    '#title'             =&gt; t('Upload Site Map (xml)'),
    '#upload_location'   =&gt; 's3://private/sitemap',
    '#field_suffix'      =&gt; 'XML format supported',
    '#upload_validators' =&gt; [
      'file_validate_extensions' =&gt; ['xml'],
    ],
    '#required'          =&gt; TRUE,
    '#validated'         =&gt; TRUE,
  ];

  $form['submit'] = [
    '#type'       =&gt; 'submit',
    '#value'      =&gt; t('Save configuration'),
    '#attributes' =&gt; [],
    '#validated'  =&gt; TRUE,
  ];

  return $form;
}
Try overriding the #attached library on the managed file field:

$form['sitemap'] = [
'#type'              =&gt; 'managed_file',
'#name'              =&gt; 'sitemap',
'#title'             =&gt; t('Upload Site Map (xml)'),
'#upload_location'   =&gt; 's3://private/sitemap',
'#field_suffix'      =&gt; 'XML format supported',
'#upload_validators' =&gt; [
  'file_validate_extensions' =&gt; ['xml'],
],
'#required'          =&gt; TRUE,
'#validated'         =&gt; TRUE,
'#attached'          =&gt; [
   'library' =&gt; [],
 ]  


];