1

I would like to select users from the Drupal users table where the date created was older than 1 day ago. Then delete those users. This code isn't correct but it will give you an idea of what I am trying to do I just don't know the right DB query. '

$users = db_select('user', 'created') //I know this is not the right DB query  
$count = count($users);
if($count > 0){
  $users = user_load_multiple(array_keys($users['user']));
}
$today = getdate(); 
if ($users < $today - '100000' ) {
 user_delete($user->uid)
}

I am thinking about using user_load_multiple() to load the users in to an array to perform the delete action.

  • 1
    Can you tell me what the raw SQL would look like? Because essentially this is an SQL question and off-topic for this stackexchange. – tenken Mar 5 '15 at 5:56
3

You can achieve this using EntityFieldQuery as shown below:

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'user')
  ->propertyCondition('created', strtotime("-1 day"), '<')
  ->propertyCondition('uid', array(0,1), 'NOT IN');
$result = $query->execute();
if(!empty($result)){
    user_delete_multiple(array_keys($result['user']));
}
| improve this answer | |

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.