Pages

Thursday, April 15, 2010

Drupal PHP Snippet: Delete users with no nodes


Here's a handy PHP snippet to purge user accounts for all users who haven't created any nodes on your site. It's useful when you have a lot of users who have signed but haven't created any nodes.
  // Get a list of all users in the site who have not created any nodes, AND uid > 1
  $sql = "SELECT u.uid FROM {users} u WHERE u.uid NOT IN (SELECT DISTINCT n.uid FROM {node} n) AND u.uid > 1 ORDER BY uid";
  $result = db_query($sql);
  while($row = db_fetch_object($result)) {
  // delete the user account
  user_delete(array(), $row->uid);
}
?>

Note: Make sure that there is a lot of cleaning up to do it times out and you see a blank page. If you add a LIMIT you could delete it batchwise.

No comments:

Post a Comment