Clear form fields after AJAX submit Given:


I am using Drupal 8.
I have a custom "add form" of the custom entity type.
Form has an ajax submission.


Problem:


When I submit form all the values remain in fields.


Expected:


Form fields should be cleared after submission.


Form source code:

&lt;?php

namespace Drupal\mymodule\Form;

use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Form\FormStateInterface;

/**
 * Form controller for the add/edit entity forms.
 */
class MymoduleForm extends ContentEntityForm {

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildForm($form, $form_state);

    $form['#prefix'] = "&lt;div id=\"{$this-&gt;getFormId()}-wrapper\"&gt;";
    $form['#suffix'] = '&lt;/div&gt;';

    $form['actions']['submit']['#ajax'] = [
      'wrapper' =&gt; $this-&gt;getFormId() . '-wrapper',
      'callback' =&gt; array($this, 'ajaxRebuildCallback'),
      'effect' =&gt; 'fade',
    ];

    return $form;
  }

  /**
   * Callback for ajax form submission.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   *
   * @return array
   *   The rebuilt form.
   */
  public function ajaxRebuildCallback(array $form, FormStateInterface $form_state) {
    drupal_set_message(t('Entity was successfully created'));

    // @todo Clear form values.
    //$form_state-&gt;setRebuild(TRUE);
    //$form_state-&gt;setValues([]);
    /*$entity = \Drupal::entityTypeManager()-&gt;getStorage('liveblog_post')-&gt;create([]);
    $form_object = \Drupal::entityTypeManager()
      -&gt;getFormObject('liveblog_post', 'add')
      -&gt;setEntity($entity);*/
    //$new_form_state = new FormState();
    //$form = \Drupal::formBuilder()-&gt;rebuildForm($this-&gt;getFormId(), $form_state);

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function save(array $form, FormStateInterface $form_state) {
    $entity = $this-&gt;getEntity();
    $entity-&gt;save();
  }

}


P.S. you can find my attempts in the ajaxRebuildCallback() method.
@Berdir's answer helped me to figure out the solution. We should indeed clear the user input, but we can not simply just say $form_state-&gt;setUserInput([]), because there are some system values, that should stay(e.g. form_id, form_token, _triggering_element_name, etc.).
I have created a custom method for it:
  /**
   * Clears form input.
   *
   * @param array $form
   *   The form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The form state.
   */
  protected function clearFormInput(array $form, FormStateInterface $form_state) {
    // Replace the form entity with an empty instance.
    $this-&gt;entity = $this-&gt;entityTypeManager-&gt;getStorage('my_entity_type')-&gt;create([]);
    // Clear user input.
    $input = $form_state-&gt;getUserInput();
    // We should not clear the system items from the user input.
    $clean_keys = $form_state-&gt;getCleanValueKeys();
    $clean_keys[] = 'ajax_page_state';
    foreach ($input as $key =&gt; $item) {
      if (!in_array($key, $clean_keys) &amp;&amp; substr($key, 0, 1) !== '_') {
        unset($input[$key]);
      }
    }
    $form_state-&gt;setUserInput($input);
    // Rebuild the form state values.
    $form_state-&gt;setRebuild();
    $form_state-&gt;setStorage([]);
  }

I am calling $this-&gt;clearFormInput($form, $form_state); in the MymoduleForm::save() method(the very last step of the submission).
Update 08.03.2021
If you have a created date field in the entity form, it will be cached in $form_state after submission. You will have to reset it's value for the entities. You can do it by overriding the \Drupal\Core\Entity\EntityForm::processForm:
 /**
   * {@inheritdoc}
   *
   * Resets default values for a new entity.
   */
  public function processForm($element, FormStateInterface $form_state, $form) {
    $element = parent::processForm($element, $form_state, $form);

    if ($this-&gt;entity-&gt;isNew()) {
      $this-&gt;entity-&gt;set('created', $this-&gt;time-&gt;getRequestTime());
    }

    return $element;
  }