If I do the usual
unset($node->myfilefield["und"][0]);
Would that take care of the actual deletion of the file as well on node_save?
No, it doesn't. You should use file_delete()
. The function will remove the attached file from the database and from the disk.
// Load the node.
$node = node_load('1');
// Get the language for the file field.
$lang = field_language('node', $node, 'myfilefield');
// load the file
$file = file_load($node->myfilefield[$lang][0]['fid']);
// unset the field for the node
unset($node->myfilefield[$lang][0]);
// delete file from disk and from database
file_delete($file);
// Save the node.
node_save($node);
The file_delete()
also takes a second argument $force
that if set to TRUE
will remove the file even if it's reported as in use by the file_usage
table.
not necessarily call file_delete(), enough:
unset($node->myfilefield["und"][0]);
node_save($node);
file_managed
table and your custom image field. It won't remove the file from disk nor will it remove the record in the file_managed
table, but thanks for the downvote.
Commented
Dec 8, 2012 at 13:35