why does user_delete() delete content while GUI does not?

rjb - July 9, 2009 - 17:40

Hi,

I use user_delete() in one of my modules, and discovered that all content of the deleted user is deleted as well. Why does this happen?

When using the GUI (at administer->users) to delete a user, the user_delete() function is also called, but the content of that user stays and gets reassigned to the anonymous user. This is what I want.

I call user_delete() this way:

<?php
$user
= user_load(array('name' => 'test'));
user_delete(null,$user->uid);
?>

The first argument is probably my problem. What do I need to put there in order to prevent deletion of the user's content?

It should behave the same

nevets - July 9, 2009 - 18:16

It should behave the same way for you. Are you sure you are loading a valid user? You should probably have a check something

if ( !empty($user) && $user->uid > 1 ) {
   user_delete(null,$user->uid);
}
?>

to make sure you have loaded a valid account. The second part is to make sure you do not delete account 0 or 1.

Thanks!

rjb - July 9, 2009 - 20:59

You're right! Account 0 was missing from the "users" table. I probably deleted it earlier because of missing safety checks. I did not know about account 0 until you mentioned it. With account 0 in place, user_delete() works like expected.

Thanks again!

 
 

Drupal is a registered trademark of Dries Buytaert.