How to delete duplicate posts with mysql queries ?
-
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!
- The topic ‘How to delete duplicate posts with mysql queries ?’ is closed to new replies.