Does Drupal automatically unlink() orphaned files? Does Drupal 7 have a way to automatically remove images that are no longer linked to an entity, or do they need to be unlinked manually?

For example, how would Drupal react to the following detachment of a file from a node:

$nid = 7;                      //A node containing an image field.
$node = node_load($nid);       //$node-&gt;field_image is a populated Imagefield.
$node-&gt;field_image = array();  //$node-&gt;field_image is set to an empty array.
node_save($node);              //$node is saved.


Will the orphaned fid sit in the files directory indefinitely or will Drupal remove it automatically?
The images will be automatically deleted if they were added through a regular image upload field. In that case Drupal understands that that image is related solely to that one node/entity.

In case you are using some other module for inserting the images, it depends and might not be deleted.