How to change field attributes of Content Entity? In a custom module, I have a content entity in which I have defined this field:  

$fields['formheader'] = BaseFieldDefinition::create('string')
  -&gt;setLabel(t('Header'))
  -&gt;setSettings([
    'max_length' =&gt; 255,
    'text_processing' =&gt; 0,
  ])
  -&gt;setDefaultValue('')
  -&gt;setDisplayOptions('view', [
    'label' =&gt; 'above',
    'type' =&gt; 'string',
    'weight' =&gt; $weight,
  ])
  -&gt;setDisplayOptions('form', [
    'type' =&gt; 'string_textfield',
    'weight' =&gt; $weight,
  ])
  -&gt;setDisplayConfigurable('form', TRUE)
  -&gt;setDisplayConfigurable('view', TRUE);


I'd like to change its type from string to string_long and max_length from 255 to 1024.
How can I do that without uninstalling my module, changing my source code and installing my module again (there is already content in this entity)?
You don't need to uninstall the module, you can write some update hooks instead.

Since it's not possible to update the field type, you have to manually move the field values to a new field.

First create a new field for the cloned values:

$fields['formheader_clone'] = BaseFieldDefinition::create('string_long')
  -&gt;setLabel(t('Header'))
  -&gt;setSettings([
    'max_length' =&gt; 1024,
    'text_processing' =&gt; 0,
  ])
  -&gt;setDefaultValue('')
  -&gt;setDisplayOptions('view', [
    'label' =&gt; 'above',
    'type' =&gt; 'string',
    'weight' =&gt; $weight,
  ])
  -&gt;setDisplayOptions('form', [
    'type' =&gt; 'string_textfield',
    'weight' =&gt; $weight,
  ])
  -&gt;setDisplayConfigurable('form', TRUE)
  -&gt;setDisplayConfigurable('view', TRUE);


Then add this update hook:

function mymodule_update_8000() {
  $entity_definition_update_manager = \Drupal::service('entity.definition_update_manager');

  // Migrate the fields and create the new field.
  $field_definition = BaseFieldDefinition::create('string_long')
    -&gt;setLabel(t('Header'))
    -&gt;setSettings([
      'max_length' =&gt; 1024,
      'text_processing' =&gt; 0,
    ])
    -&gt;setDefaultValue('')
    -&gt;setDisplayOptions('view', [
      'label' =&gt; 'above',
      'type' =&gt; 'string',
      'weight' =&gt; $weight,
    ])
    -&gt;setDisplayOptions('form', [
      'type' =&gt; 'string_textfield',
      'weight' =&gt; $weight,
    ])
    -&gt;setDisplayConfigurable('form', TRUE)
    -&gt;setDisplayConfigurable('view', TRUE);
  $entity_definition_update_manager-&gt;installFieldStorageDefinition('formheader_clone', 'my_entity', 'my_module', $field_definition);

  drupal_flush_all_caches();

  $entities = \Drupal::entityTypeManager()-&gt;getStorage('my_entity')-&gt;loadMultiple();
  foreach ($entities as $entity) {
    $entity-&gt;formheader_clone-&gt;value = $entity-&gt;formheader-&gt;value;
    $entity-&gt;save();
  }

  // Delete the storage definition if it was defined before.
  $storage_definition = $entity_definition_update_manager-&gt;getFieldStorageDefinition('formheader', 'my_entity');
  if ($storage_definition) {
    $entity_definition_update_manager-&gt;uninstallFieldStorageDefinition($storage_definition);
  }
}


Now run drush updb

Now you have to update the formheader field in your custom entity, this is what it must look like:

$fields['formheader'] = BaseFieldDefinition::create('string_long')
  -&gt;setLabel(t('Header'))
  -&gt;setSettings([
    'max_length' =&gt; 1024,
    'text_processing' =&gt; 0,
  ])
  -&gt;setDefaultValue('')
  -&gt;setDisplayOptions('view', [
    'label' =&gt; 'above',
    'type' =&gt; 'string',
    'weight' =&gt; $weight,
  ])
  -&gt;setDisplayOptions('form', [
    'type' =&gt; 'string_textfield',
    'weight' =&gt; $weight,
  ])
  -&gt;setDisplayConfigurable('form', TRUE)
  -&gt;setDisplayConfigurable('view', TRUE);


Now add this update hook:

function mymodule_update_8001() {
  $entity_definition_update_manager = \Drupal::service('entity.definition_update_manager');

  $field_definition = BaseFieldDefinition::create('string_long')
    -&gt;setLabel(t('Header'))
    -&gt;setSettings([
      'max_length' =&gt; 1024,
      'text_processing' =&gt; 0,
    ])
    -&gt;setDefaultValue('')
    -&gt;setDisplayOptions('view', [
      'label' =&gt; 'above',
      'type' =&gt; 'string',
      'weight' =&gt; $weight,
    ])
    -&gt;setDisplayOptions('form', [
      'type' =&gt; 'string_textfield',
      'weight' =&gt; $weight,
    ])
    -&gt;setDisplayConfigurable('form', TRUE)
    -&gt;setDisplayConfigurable('view', TRUE);
  $entity_definition_update_manager-&gt;installFieldStorageDefinition('formheader', 'my_entity', 'my_module', $field_definition);

  drupal_flush_all_caches();

  $entities = \Drupal::entityTypeManager()-&gt;getStorage('my_entity')-&gt;loadMultiple();
  foreach ($entities as $entity) {
    $entity-&gt;formheader-&gt;value = $entity-&gt;formheader_clone-&gt;value;
    $entity-&gt;save();
  }

  // Delete the storage definition if it was defined before.
  $storage_definition = $entity_definition_update_manager-&gt;getFieldStorageDefinition('formheader_clone', 'my_entity');
  if ($storage_definition) {
    $entity_definition_update_manager-&gt;uninstallFieldStorageDefinition($storage_definition);
  }
}


Now run drush updb again

It's important to do it in the correct order