• Resolved Louno

    (@louno)


    Hi,
    Something went wrong on one of my wordpress site, some script got stuck in a loop and created a huuuuuge amount of duplicate posts…

    I tried using various plugins to detect and delete duplicate posts but none have worked so far, they all make the server crash (which then requires me to contact hosting support to reset the server, otherwise it just stays offline). Note that the site is running an older version of wordpress (3.2) and so there are a bunch of plugins I cannot try, but I believe that the best would be to directly go into phpmyadmin and run some queries…

    I do not know how to use sql queries but googled and found this code that allows to detect/display duplicate post, it took 10-15 minutes to run and found 65k duplicates:

    SELECT a.ID, a.post_title, a.post_type, a.post_status
    FROM wp_posts AS a
       INNER JOIN (
          SELECT post_title, MIN( id ) AS min_id
          FROM wp_posts
          WHERE post_type = 'post'
          AND post_status = 'publish'
          GROUP BY post_title
          HAVING COUNT( * ) > 1
       ) AS b ON b.post_title = a.post_title
    AND b.min_id <> a.id
    AND a.post_type = 'post'
    AND a.post_status = 'publish'

    Although very slow, it did work, and so the next step is to actually delete the duplicates, this is the code I tried:

    DELETE a.*
    FROM wp_posts AS a
       INNER JOIN (
          SELECT post_title, MIN( id ) AS min_id
          FROM wp_posts
          WHERE post_type = 'post'
          AND post_status = 'publish'
          GROUP BY post_title
          HAVING COUNT( * ) > 1
       ) AS b ON b.post_title = a.post_title
    AND b.min_id <> a.id
    AND a.post_type = 'post'
    AND a.post_status = 'publish'

    I’ve tried this code a few times now but it always crash the server… I believe that the issue is that it tries to do everything in 1 pass… would it be possible to add some sort of variable to this query so that I can specify to delete only say 100 duplicates or 1000 duplicates, this way it would reduce the impact on the server and I can run the query a few times until everything is deleted.

    Hopefully there’s a way to do this…

    Thanks for your help!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Louno

    (@louno)

    Ok, I sort of solved my problem… after trying a few times I was successful in running the mysql query to delete all duplicates without the server crashing. That didnt solve my problem though, after optimizing my database was still over 2gb, all in the wp_posts table.

    I then installed this plugin https://www.ads-software.com/plugins/wp-clean-up-optimizer/ and realized that the database contained hundred of thousands of revisions as well as Orphan Posts Meta and Orphan Relationships, overall well over 2 million entries… The plugin wasnt able to delete them (way too slow) so I figured out the specific sql queries to do this manually in phpmyadmin and it worked!

    Went from 2gb database to 235mb! that is incredible, the website is A LOT faster to load.

    Hi Louno,
    thanks for your post, very interesting. Can you write the query you made to manually remove revisions?
    Thank you very much!
    Edo

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to delete duplicate posts with mysql queries ?’ is closed to new replies.