How to automatically update entity that is created programatically? Using Entity API, I can create entities easily like this:

// SomeEntity class extends ContentEntityBase
$entity = SomeEntity::create(['my_id' =&gt; 'id123']);
$entity-&gt;save();


Works fine as long as my_id is unique. When it's not, I get EntityStorageException with message complaining about my_id not being unique. I could first try to load entity to see if it exists and update it manually but that seems like unnecessary overhead.

Is there a way to automatically save or update existing entity programatically?
Not really. To figure out if an entity exists, you have to check that somehow.

If you're trying to optimize, you could run an entity query instead of loading it completely. But in case you want to update, you need to load the original entity anyway, so just do that.

$entity = SomeEntity::load(['my_id' =&gt; 'id123']);
if (!$entity) {
  $entity = SomeEntity::create(['my_id' =&gt; 'id123']);
}
$entity-&gt;save();