After creating users with a picture programmatically, and deleting them afterwards, I found out that the pictures (files) were not removed.

I started digging into this problem and found out that this problem also occurs when just adding a user by the UI, uploading a picture and deleting the user.

The picture is not being removed. Also the file remains in the file_usage and in the file_managed tables. They're not marked for deletion, so I guess they will stay there forever. Is this a bug? Or am I missing something?

  • This one is a new one to me; I've definitely seen this as a common complaint with respect to deleted nodes and their attached files sitting around forever (the culprit is usually because revisions are enabled). It does appear to be a bug, though: drupal.org/node/1398616 – Weston Wedding Aug 13 '15 at 15:58
  • Have there been a cron run between user deletion and the moment you tested for leftover files? If no, can you see if cron would delete them? – Mołot Aug 13 '15 at 16:21
  • Yes, cron has run multiple times. But as Weston pointed out; it looks like an existing bug. – Rene Aug 13 '15 at 16:25
  • I don't call it a bug , drupal just does not delete files cause deleting files may lead to errors in future , maybe they are used somewhere else – Alireza Tabatabaeian Aug 13 '15 at 17:24

if you really like to delete user profile picture on user delete event use this code in hook_user_delete($account).

// Load user by using user id
$user = user_load($uid);
// Get file id of profile photo
$fid = $user->picture->fid;
// Load the file object
$file = file_load($fid);
// delete profile photo from.
// Unset the image object (my case it's 'picture')
// Save the user

hope works for you

|improve this answer|||||

Reference : #3 I'd be interested in opinions on a couple of approaches to this:

1. Modify user.module user_delete function to directly call file_usage_delete and file_delete if there is a profile picture attached to the user account (which would solve this individual issue)

2. As there would appear to be the possibility that any files which belong to the user may not be deleted when the user is deleted via user_delete_multiple, would a better approach be to implement hook_user_delete in the file module (file_user_delete) so that the file module will respond to the user delete by deleting any files associated with them?

As an aside - and might need a separate issue, as far as I can see the code for user_cancel applies the same to "Delete user and all their content" and "Delete user and assign their content to anonymous". Do we need to cover this scenario too?

Hope this help you well.

|improve this answer|||||
  • Although I think that the solution provided in drupal.org/node/1398616#comment-5757520 is a more solid way to solve this problem, I prefer using the code provided by @alireza-tabatabaeian. – Rene Aug 16 '15 at 17:05

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.