• Resolved woorooo

    (@woorooo)


    Hi,

    Is it safe to use this script to cleanup UsersWP user database from users that didn’t comment of posted anything?

    Thank you.

    <?php
    include("wp-config.php");
    
    $usertable=$table_prefix."users";
    $commentable=$table_prefix."comments";
    $usermeta=$table_prefix."usermeta";
    $postable=$table_prefix."posts";
    $linktable=$table_prefix."links";
    
    $db_handler=mysql_connect(DB_HOST, DB_USER, DB_PASSWORD)
    or die("MySQL database '".DB_NAME."' not accessible.");
    
    mysql_select_db(DB_NAME, $db_handler)
    or die("Enable to select ".DB_NAME." database
    \n");
    
    $query1="DELETE FROM $usertable 
             WHERE ID > 1 
             AND ID NOT IN (SELECT DISTINCT post_author FROM $postable) 
             AND ID NOT IN (SELECT DISTINCT user_id FROM $commentable)";
    
    $query2="DELETE FROM $usermeta 
             WHERE user_id > 1 
             AND user_id NOT IN (SELECT DISTINCT post_author FROM $postable) 
             AND user_id NOT IN (SELECT DISTINCT user_id FROM $commentable)";
    
    $query3="DELETE FROM $linktable 
             WHERE link_owner > 1 
             AND link_owner NOT IN (SELECT DISTINCT post_author FROM $postable) 
             AND link_owner NOT IN (SELECT DISTINCT user_id FROM $commentable)";
    
    mysql_query($query1,$db_handler);
    mysql_query($query2,$db_handler);
    mysql_query($query3,$db_handler);
    
    echo "Done!";
    ?>

    Source: https://www.scriptol.com/wordpress/mass-delete-users.php

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi,

    Can you explain in detail what exactly you want to clean? We use our own table for storing user meta called ‘wp_uwp_usermeta’ on registration. Users are stored in default ‘wp_users’ table.

    Regards,
    Patrik

    Thread Starter woorooo

    (@woorooo)

    Hi Patrik,

    I want to be able to delete users from time to time that don’t have any comments or any posts, including custom post types like Geodirectory. Is there a way to do it?

    Hi,

    The query seems OK to me but you should do it in a WordPress way using WordPress functions and you can use a cron job for this which will allow doing this on some intervals also generally wp_links table doesn’t have any data stored but if you are using any plugins who store data then you can use that query. I would suggest first you do on the staging or local site instead of doing on the live site.

    Regards,
    Patrik

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Cleanup Users without comments or posts’ is closed to new replies.