How can I update the length of a base field definition in an update hook? I'm struggling with writing an update hook that will simply update the length of a text base field attached to an entity of mine.

Here's my update hook:

function my_module_update_8001() {
  // Collect all existing story run data.
  $database = \Drupal::database();

  // Gather all the existing story run data.
  $tables = [
    'content_tile_field_data',
    'content_tile_field_revision',
  ];
  $existing_data = [];
  foreach ($tables as $table) {
    $existing_data[$table] = $database-&gt;select($table)
      -&gt;fields($table)
      -&gt;execute()
      -&gt;fetchAll(PDO::FETCH_ASSOC);
    $database-&gt;truncate($table)-&gt;execute();
  }

  // Update the "name" base field definition to a new max length
  $update_manager = \Drupal::entityDefinitionUpdateManager();
  $name_field = $update_manager-&gt;getFieldStorageDefinition('name', 'content_tile');
  $name_field-&gt;setSetting('max_length', 100);
  $update_manager-&gt;updateFieldStorageDefinition($name_field);

  // Restore the data.
  foreach ($tables as $table) {
    $insert_query = $database
      -&gt;insert($table)
      -&gt;fields(array_keys(end($existing_data[$table])));
    foreach ($existing_data[$table] as $row) {
      $insert_query-&gt;values(array_values($row));
    }
    $insert_query-&gt;execute();
  }
}


After running this, the name field is clearly dropped and re-added (I know because it now appears at the end of the list of columns in the table), but the length is still set to 50, the old value.

I've narrowed this down to BaseFieldDefinition::getSchema():

  public function getSchema() {
    if (!isset($this-&gt;schema)) {
      // Get the schema from the field item class.
      $definition = \Drupal::service('plugin.manager.field.field_type')-&gt;getDefinition($this-&gt;getType());
      $class = $definition['class'];
      $schema = $class::schema($this);
      // Fill in default values.
      $schema += array(
        'columns' =&gt; array(),
        'unique keys' =&gt; array(),
        'indexes' =&gt; array(),
        'foreign keys' =&gt; array(),
      );

      // Merge custom indexes with those specified by the field type. Custom
      // indexes prevail.
      $schema['indexes'] = $this-&gt;indexes + $schema['indexes'];

      $this-&gt;schema = $schema;
    }

    return $this-&gt;schema;
  }


The schema definition is cached, so the update process gets the old schema data instead of regenerating it. There appears to be no way to clear this internal cache.

If I comment out the code related to the caching, it works as expected.

Is there another approach to this that I'm missing?

EDIT: The approach I took instead is to just load the current definition and pass that through to the update manager, like this:

  $update_manager = \Drupal::entityDefinitionUpdateManager();
  $definitions = \Drupal::entityManager()-&gt;getFieldStorageDefinitions('content_tile');
  $name_field_definition = $definitions['name'];
  $update_manager-&gt;updateFieldStorageDefinition($name_field_definition);


I guess this is not recommended normally, but it worked just fine.
I was having the exact same issue, trying to update the "max_length" of the name field of a custom entity.
I did a hook_update like that :

function mymodule_update_8005() {
  $entity_definition_update_manager = \Drupal::service('entity.definition_update_manager');
  $field_definition = $entity_definition_update_manager-&gt;getFieldStorageDefinition('name', 'myentityid');
  $field_definition-&gt;setSetting('max_length', 255);
  $entity_definition_update_manager-&gt;updateFieldStorageDefinition($field_definition);
}


But then, even if the fieldDefinition had correctly changed, I was still having the old length limitation.

Finally, what I had forgotten to do was changing this max_length in my entity definition itself, here :

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
  $fields['name'] = BaseFieldDefinition::create('string')
  -&gt;setLabel(t('Title'))
  -&gt;setSettings(array(
    'max_length' =&gt; 255,
    'text_processing' =&gt; 0,
  ));
  ...


After that, it worked as desired.