Delete an entity reference field programmatically

I found this code useful to delete a value saved as an entity reference:

$customer = Customer::load($form_state->getValue('customer_id'));
$customer->get('field_contact_person')->removeItem(0);
$customer->save();

 

 

Saved you some valuable time?

Buy me a drink 🍺 to keep me motivated to create free content like this!