1

I have a node to which I have a number of images attached through the usual content type image field mechanism. The attachment is done correctly, such that when I delete the node, the images are also deleted.

My problem is that these images are held in a subdirectory, which is not deleted by the file deletion process, and I end up with a bunch of empty and useless directories littering my file system. I tried deleting the directory with hook_node_delete(), but this fails because Drupal hasn't yet gotten around to deleting the images when that hook gets called, and the rmdir() function I was using to delete the directory fails because the directory isn't empty.

So: Is there a hook I can call that will run after the files have been deleted? I suppose I could write my own code in hook_node_delete() to delete the files and then the directory, but I'd rather not hack around the architecture like that, if possible.

  • According to the docs, This hook is invoked from node_delete_multiple() after the type-specific hook_delete() has been invoked, but before hook_entity_delete and field_attach_delete() are called, and before the node is removed from the node table in the database. so have you tried doing this in api.drupal.org/api/drupal/modules%21field%21field.attach.inc/… ? – Jimajamma Jan 26 '14 at 18:58
  • Aha -- mymodule_field_attach_delete works nicely -- thanks! If you post this as an answer, I'll accept it. – Jim Miller Jan 26 '14 at 22:28
2

You may want to look into hook_field_attach_delete(). This runs after all fields have been removed from a node/entity. So, if all the file fields (and the actual files) have been removed, and all of them in such a node are all in one directory and they are the only files in that directory, removing that directory in this hook should succeed.

  • Works fine for me (although I see the value in the _file_delete() approach) – Jim Miller Jan 26 '14 at 23:00
1

You could try instead using

hook_file_delete

Respond to a file being deleted. - $file: The file that has just been deleted.

e.g:

/**
 * Implements hook_file_delete
 */
function MYMODULE_file_delete($file) {
  // Attempt to delete empty file directory, on error resume next
  @drupal_rmdir(dirname(drupal_realpath($file->uri)));
}

Might not need drupal_realpath, but that should work.

  • Thanks / interesting, but I think the field_attach_delete is cleaner. – Jim Miller Jan 26 '14 at 22:29
  • hook_field_attach_delete is for acting on all attached fields, hook_file_delete is specifically for acting on files only. That suggests the latter is simpler / cleaner IMO. – David Thomas Jan 26 '14 at 22:39
  • I think both work equally cleanly; but this one will make sure the directory is really removed regardless of what other logic is out there in the system, eg, no matter what, when you remove the last file in that directory, the directory will get removed as well. – Jimajamma Jan 26 '14 at 22:51

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.