How to remove all roles from a user? Users on my site generally have several roles.  However, every once in awhile I get a spam user, and so I close that account by flagging it with a spam flag, which triggers a rule to block the account.

Since I compile statistics on my site based on the number of users having a given role, I don't want spam users to be included.  How can I automatically remove all roles from spam users?  (I suppose I could add rules components to check for each role and remove it if found, but there has to be a more efficient way...)
This function can help you

/**
 * Remove a role from a user.
 *
 * @param $user
 *   User object or user ID.
 * @param $role_name
 *   String value of role to be removed.
 */
function custom_remove_role_from_user($user, $role_name) {
  // For convenience, we'll allow user ids as well as full user objects.
  if (is_numeric($user)) {
    $user = user_load($user);
  }
  // Only remove the role if the user already has it.
  $key = array_search($role_name, $user-&gt;roles);
  if ($key == TRUE) {
    // Get the rid from the roles table.
    $roles = user_roles(TRUE);
    $rid = array_search($role_name, $roles);
    if ($rid != FALSE) {
      // Make a copy of the roles array, without the deleted one.
      $new_roles = array();
      foreach($user-&gt;roles as $id =&gt; $name) {
        if ($id != $rid) {
          $new_roles[$id] = $name;
        }
      }
      user_save($user, array('roles' =&gt; $new_roles));
    }
  }
}


first load user's roles, then call this function  something like

$user=user_load($uid);
foreach($user-&gt;roles as $role){
custom_remove_role_from_user($user, $role)
}