I have content type with Photos ImageField. I need to create some moderation view to show all photos and "delete" link to delete appropriate photo. How can I programmatically delete value from CCK field (in general not only ImageField but any field type)?


You will need to check the ImageField array to see what needs to be changed but for D6 but it should be something like the below:

$node = node_load($nid);
$node->field_photo[0]['value'] = NULL;

Just change the field name to what you are using and change the $nid to the node id #.

  • This appears to be the best approach in Drupal 7 too. There are heaps of field functions, but none of them seem able to single out and delete one specific field entry. – user56reinstatemonica8 Apr 4 '12 at 1:15

WestieUK has a correct general expression. However, the vague question presents two potential cases for Filefield/Imagefield here:

  1. Delete all references to that file from all nodes -> filefield_file_delete($file);.
  2. Delete only that node's reference to the file -> Use WestieUK's method.

If there never are multiple node associations for the files in your structure (usual behavior in Filefield/Imagefield), method #1 is safe and simple to use for case #2 as well.
If the file is no longer referenced, do not forget to also remove the entry from the file tables and delete the file from disk with file_delete($path);.

Your Answer

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