I have some nodes of a content type with following fields:

  1. title
  2. body
  3. image

I want to programmatically (!important) set the body field as empty (to make it like it was never set with any value).

This is what I have done:

$this_node = node_load($nid);
$this_node->body = array(); //to set it as an empty field

After executing this, the node is not saved. Instead, in db_log following error is loged:

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '104418'
for key 'PRIMARY': INSERT INTO {node} (nid, vid, type, language, title, uid, status, created,
changed, comment, promote, sticky, tnid, translate, uuid) VALUES
(*******placeholders & their values*******) in drupal_write_record()
(line 7170 of /var/www/rotary.local/docroot/includes/common.inc).

Is there someting I am missing? How I can set the body field as empty?


I don't want to use field_attach_update for caching purposes.

  • 2
    Perhaps this would work = $this_node->body[$this_node->language] = array(); – Cyclonecode Feb 17 '14 at 10:05
  • @KristerAndersson That's spot on, you should put it in as an answer – Clive Feb 17 '14 at 10:10
  • @KristerAndersson: It does work. But when I save the content originally with no body field then $node array shows $node->body as an empty array. However, using the logic you suggested, it shows $node->body as an array with $node->body[language] as empty array. I am worried this may not be ideal. Could you please support your answer with something that can convince me that it is safe to do so. – kmdhrm Feb 17 '14 at 10:11
  • @Clive: Why does saving $this_node->body as empty array gives the PDOException error, while saving $this_node->body[$this_node->language] as empty array does not give the error – kmdhrm Feb 17 '14 at 10:14
  • Because the first method is incorrect, and the second method is correct :) Basically with the first method you're not providing a language code, which is necessary for the field API, and incorrect data is getting into the database. Next time you try to do the same thing, integrity constraints fail, and you get the error. – Clive Feb 17 '14 at 10:19

To remove the value from you field you should do like this before saving it:

$this_node->body[$this_node->language] = array();
| improve this answer | |
  • After testing the code with your suggestion I found that it still gives the error. However I am able to save the node using field_attach_update. What could be causing this? – kmdhrm Feb 17 '14 at 10:31
  • @DipakYadav - That seems very strange, what error do you get? – Cyclonecode Feb 17 '14 at 10:57
  • The error was being caused because of a whole lot different reason. Please see the comments to the question. – kmdhrm Feb 17 '14 at 11:36

To make body field empty you should implement hook_node_presave() and inside this, use-

   $node->body[LANGUAGE_NONE][0]['value'] = '';
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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