BuddyDev

Search

BP User Datatabase Cleanup

  • Participant
    Level: Guru
    Posts: 501
    Daniel on #55407

    Hi guys,

    I hope you are doing well.

    I have to clean up the BP User database because it slows down the website and I have about 100’000 spam users to clean.

    Could you provide any good plugin or technique to delete such spam users? The Bulk Delete Plugin https://wordpress.org/plugins/bulk-delete/ is in the meanwhile outdated and I wanted to ask if there is any better plugin you could recommend?

    The other option would be to use AI generated code to implement it in the functions.php file. Would you recommend to run this code or better not?

    
    // Schedule the custom event on plugin/theme load
    function schedule_avatarless_user_cleanup() {
        if (!wp_next_scheduled('delete_avatarless_users_event')) {
            wp_schedule_event(time(), 'every_three_minutes', 'delete_avatarless_users_event');
        }
    }
    add_action('wp', 'schedule_avatarless_user_cleanup');
    
    // Custom interval: every 3 minutes
    function custom_cron_schedule($schedules) {
        $schedules['every_three_minutes'] = array(
            'interval' => 180, // 3 minutes in seconds
            'display'  => esc_html__('Every 3 Minutes'),
        );
        return $schedules;
    }
    add_filter('cron_schedules', 'custom_cron_schedule');
    
    // Deletion logic: delete 25 users without avatars
    function delete_avatarless_users_batch() {
        $args = array(
            'meta_key'   => '_has_avatar',
            'meta_value' => '0',
            'number'     => 25,
            'fields'     => 'ID',
        );
        $users = get_users($args);
    
        foreach ($users as $user_id) {
            wp_delete_user($user_id);
        }
    
        if (empty($users)) {
            // All done – clear the event
            wp_clear_scheduled_hook('delete_avatarless_users_event');
        }
    }
    add_action('delete_avatarless_users_event', 'delete_avatarless_users_batch');

    Thank you for your feedback.

    Regards
    Daniel

  • Keymaster
    (BuddyDev Team)
    Posts: 24968
    Brajesh Singh on #55420

    Hi Daniel,
    Welcome back.

    I will not recommend the AI generated code as It has atleast 2 problems(I did not read thew whole code after that)

    1. Usage of _has_avatar. BuddyPress does not store the flag. If you are using one of our plugin such as profile completion, this data might be available.

    2. There is no need to hook into wp for scheduling. It could be done as one time activity on activation and cleaned on deactivation.

    Did you face any issue with the bulk-delete plugin?

    Regards
    Brajesh

  • Participant
    Level: Guru
    Posts: 501
    Daniel on #55426

    Hi Brajesh,

    I was going to install it but then I read the negative user reviews that the plugin broke the website’s database when bulk-deleting and also there is the message from WordPress on top of the plugin page that makes me not try this out because what if database techniques have been changed since the last 3 WordPress releases?

    This plugin hasn’t been tested with the latest 3 major releases of WordPress. It may no longer be maintained or supported and may have compatibility issues when used with more recent versions of WordPress.

    I am still searching for a plugin that can bulk-delete users by meta data. Since I am using your plugin “profile completion” do you think if I go to phpmyadmin and run a sql query that could make the job done or is it not recommended?

    Thanks for your update.

    Regards
    Daniel

    • This reply was modified 5 days, 2 hours ago by Daniel.
  • Keymaster
    (BuddyDev Team)
    Posts: 24968
    Brajesh Singh on #55431

    Hi Daniel,
    Thank you for the reply.

    The problem is lack of awareness of the context while cleaning database tables. Whether you do it manually or use a plugin like Bulk Delete, if you are not aware of the entity relationship in database, It will have consequences.

    In your case, the users may have data in profile fields, activities, groups and you need to clean that.

    The best way would be to find all the user ids who lack avatar and then delete the users in batch(small batches). This is inline with your proposed initial solution. If you delete/force delete the user, BuddyPress will clear data for them. In this case, you do not need to know about various entity relationships in the database.

    Regards
    Brajesh

  • Participant
    Level: Guru
    Posts: 501
    Daniel on #55442

    Hi Brajesh,

    I have installed the Bulk Delete Plugin as it seems the only solution and I saw that I can bulk delete users with meta data. I am using the BP Profile Completion plugin from BuddyDev and there are two tags that show up in Bulk Delete Plugin:

    has_avatar
    _has_avatar

    Which meta of the two do I need to choose to delete all users who do not have avatar?

    To run the command I use let’s say the meta _has_avatar or has_avatar and then I choose from the drop down menu “NOT CONTAINS” so that means the Bulk Delete plugin will delete all users who do not contain _has_avatar or do I need to choose “CONTAINS”? Do I also need to write something in the field “Meta Value” or can I leave it blank?

    Thanks a lot for your help.

    Regards
    Daniel

You must be logged in to reply to this topic.

This topic is: not resolved