• I have a website with about 350,000 posts which I need to thin by category and date, but whenever I try a bulk deletion plugin the script times out. Is there a SQL statement you guys could post which will delete all posts older than a specific date using mysql?

    Could you also post one which will do the same thing by category and date?

Viewing 7 replies - 1 through 7 (of 7 total)
  • For such tasks I would recommend to use the WP CLI: https://wp-cli.org – it runs server-side and does not know timeouts. However, in your case it requires some custom development and you should be able to connect to the console on the server. Doesn’t work in every hosting unfortunately.

    Alternatively you could do it via WP-Cron, also with an individual development that deletes e.g. every 5 minutes 50 records. Takes a bit longer – and there is still the risk of a timeout if you use WP-Cron and do not switch to system cron.

    Another alternative would possibly be cleanup plugins that help you delete certain records. But also here is the problem that it comes to a timeout.

    What I would advise against I delete directly in the database. Each record has links to other tables, e.g. for posts records the postmeta properties. This would have to be retrieved as well, which is why deleting directly in the database is extremely unfavorable.

    Thread Starter wpbroken2much

    (@wpbroken2much)

    How do I delete using CLI?

    Take a look at the manual: https://developer.www.ads-software.com/cli/commands/post/delete/

    And always remember to make a backup before ??

    Thread Starter wpbroken2much

    (@wpbroken2much)

    Thanks, but that link is more of what I’m talking about. It lists just three specific examples which are the deletion of a single post, all pages, and all posts in the trash. Then it has big list of parameters which assumes the reader understands the syntax.

    They need to add an example for deleting a range of posts from a specific category. If I want to remove all posts from a category named Uncategorized between post number 1 and post number 50,000. Deleting all posts in a specific category before a specific post would also work for me in this case.

    Thread Starter wpbroken2much

    (@wpbroken2much)

    Again, this thread shows what I’m talking about. Rather than simply respond to my inquiry with a snippet showing how to specifically delete all posts in a single category or before a specific date/ID I get nothing but a general link to use CLI.

    That CLI link doesn’t tell me how to do what I need to do. It seems all too often staff members of companies like WordPress will only respond with links to broad things that might point in the right direction but without going far enough to actually work.

    Please have a WordPress staff member post exactly how to remove all posts in a specific category or before a specific date/ID number. I should have to research how to use CLI, learn the syntax, and figure it out for myself when surely a WordPress staff member could post exactly how to do it.

    First:
    This forum is used by volunteers to help other wordpress users. I am not a “staffmember” of anything but volunteer here and answer your as well as other questions in my spare time – free of charge. You can read more about it here: https://www.ads-software.com/support/guidelines/

    If you are looking for professional support, you can find it here: https://jobs.wordpress.net

    And now to your request:
    The page describing wp user doesn’t quite contain what you need, of course. Your requirement is very individual, so there is nothing ready-made for it. But thanks to the many possibilities that many thousands of volunteers put into the development of wordpress, what you want is still doable. You need to manually add a function here that can be executed via WP CLI.

    What you need for this is documented (also by volunteers) in the WordPress manual. E.g. here is a function with which you can add your own command to the CLI:
    https://make.www.ads-software.com/cli/handbook/references/internal-api/wp-cli-add-command/

    Relevant for the determination of the users is a WP_User_Query, whose options are described here: https://developer.www.ads-software.com/reference/classes/wp_user_query/

    Here is a small example I just wrote, but I couldn’t test it because I don’t have a database with thousands of users:

    function custom_cli() {
    	WP_CLI::add_command('custom_cli', 'custom_user_delete');
    }
    add_action( 'cli_init', 'custom_cli' );
    
    function custom_user_delete() {
        $users = get_users([
            'role' => 'editor', // select only users with the editor-role
            'date_query' => array( // select only users which has been registered during the last 12 months
    		    array( 'after' => '12 months ago', 'inclusive' => true )
    	    )
        ]);
        foreach( $users as $user ) {
            wp_delete_user($user->ID);
        }
    }

    You would have to add that to your theme’s functions.php, your custom plugin or via code snippet plugin. After that you should call the console with

    wp custom_cli

    to call the deletion process. But be careful: You have to adjust the commented part with the condition.

    And always remember to make a backup.

    Thread Starter wpbroken2much

    (@wpbroken2much)

    I found a way to delete everything from a specific category but the category has over 30,000 posts has been running for over 3 possibly 4 days and only appears slightly over half way done.

    wp post delete $(wp post list --cat=8 --format=ids)

    Then I will try this other one which I think will remove posts from a specific category for a specific year. I’d like to add the month to this one if possible.

    wp post delete $(wp post list --post_type=post --cat=8 --format=ids --year=2016)

    I expect the above to work at a snail’s pace which is why I’d love to find a SQL query that I can use in MySQL.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How Do I Bulk Delete Tens of Thousands of Posts?’ is closed to new replies.