Skip to main content

SQL Queries

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.

<?php
 
// 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);
}
?>
Syndicate content