Deleting a filefield content and the associated file

Public

This code will delete the filefield content and the associated file.

Get raw version
php
  1. // Load the node.
  2. $node = node_load('1');
  3. // Get the language for the file field.
  4. $lang = field_language('node', $node, 'myfilefield');
  5. // load the file
  6. $file = file_load($node->myfilefield[$lang][0]['fid']);
  7. // unset the field for the node
  8. unset($node->myfilefield[$lang][0]);
  9. // delete file from disk and from database
  10. file_delete($file);
  11. // Save the node.
  12. node_save($node);