Suppose I have an entity with a field field_foo on it and I want to clear that field's value.

What should I pass to $wrapper->set()?

I have tried NULL and array() and both produce an error message.


4 Answers 4


The problem is that you must set an empty value that is compatible with the data type of your field. If you do not, you will get an exception raised. Passing NULL or array() when a string is expected will therefore raise an error.

Another thing to keep in mind is that the data you pass will also depend on whether your field is a single value, a multi-value field, or a field with multiple properties.

If your field is a single value (and thus the wrapper for the field is an instance of EntityValueWrapper) then you should assign it an empty value compatible with the data type in question. For instance the two following methods are equivalent:

$wrapper->title = '';

However the following three examples will raise an exception, because the data types are not compatible with the title field:


If your field is a field with multiple properties (for instance a formatted text field, which defined both a value and format property) and thus an instance of EntityStructureWrapper, then array() or NULL will be the correct empty value. So you can do the following:

$wrapper->field_formatted_text = array();
$wrapper->field_formatted_text = NULL;

But in that case, passing an empty string would have raised an error. Note that you could have chosen to make the value property empty instead, in which case a string is the correct data type:

$wrapper->field_formatted_text->value = '';

Finally, if your field is a multi-value field (and thus your wrapper is an instance of EntityListWrapper) then array or NULL are the correct empty values, and the following three lines are equivalent:

$wrapper->field_example_multiple = array();
$wrapper->field_example_multiple = NULL;

Note: Calling the clear method on the wrappers is not equivalent to setting the field to an empty value. When the field is set to an empty value, it then calls EntityMetadataWrapper::updateParent on the field's parent wrapper. This ensures amongst other things that the setter callback defined by hook_entity_property_info is called. Calling clear does not do that.

  • 1
    Note that if the field is multiple and required, setting as array() or NULL can fail, because the field can't be empty. This is different from normal $node field assignment, where you can programmatically save an empty required field (it just won't save via Drupal's own UI). In this case, a workaround is array(N), where N is the ID of a non-existent yet referenced entity. Note that it will save with that ID, so your data is arguably "broken" in a relational sense; but it shouldn't affect the theme layer if you're doing all the right things there (e.g. using Display Suite or Panels).
    – J-P
    Commented Mar 9, 2015 at 10:30
  • $w->field_allowed_regions->set(array(null)); is the only option that worked for my multi-value taxonomy reference field.
    – Incredible
    Commented Nov 21, 2016 at 14:38
  • In my case I have an entity reference field with a single value. The following worked for me: $wrapper->field_entity_reference->set(NULL); Commented Apr 14, 2018 at 19:34

Further to other answers and comments, if the field is multiple and required, as previously noted you cannot use



nor even $wrapper->field_example_multiple->set(array()),

but instead you can use the following if you want to clear the field of all its values:


In fact, this works whether or not the multiple-value field is set to 'required', and so I'd recommend always using this, to ensure that your code is robust.

(Of course, if the field is 'required' then you perhaps shouldn't be completely clearing it anyway, but your code might be doing this as a preliminary step to deleting the whole entity or something similar, so there are times when it might just be legitimate.)

  • Be aware that using ` $wrapper->field_example_multiple->set(array(NULL))` will lead to having a NULL element in the data array. This method does not clear values but rather sets values array to a single NULL value. Commented Mar 1, 2016 at 23:35
  • Good point. I guess that returns us to my remark about not clearing a required value. It is probably made impossible intentionally.
    – Martin Q
    Commented Mar 18, 2016 at 11:41
  • In point of fact, a required field must have at least one non-null value. If you want to reset a required multivalued field, just overwrite it with the new value. I.e.: $product_display->field_product = array($product_id); Commented Mar 27, 2016 at 19:42

It appears that the complexities listed in the other comments are only relevant to a required field. If the field is not required, then this should be pretty simple:

$wrapper->field_foo = NULL;

You can use the wrapper to check the properties of the field:

$properties = $wrapper->getPropertyInfo();
$field_required = !empty($properties['field_foo']['required']);

Depending on the context, you can also just get the properties of the one field using:


Another solution for this problem could be EntityMetadataWrapper::clear


  • EntityMetadataWrapper::clear method is declared as "protected", so it can not be called from your code: only "public" methods can be accessed directly from outside of the object. Commented Mar 27, 2016 at 18:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.