I have a client who has a massive build-up of custom posts that needed to be deleted from their WordPress installation. We are running the GeoDirectory plugin and need to clear out about 230,000 places/locations (230,881 to be exact!).
Even plugins like Bulk Delete aren’t powerful enough to efficiently handle that kind of a deluge. I considered going directly into the database via phpMyAdmin and just running an SQL command to drop those entries from the table. But then I thought about all the orphaned entries that may be left behind that are associated with every custom post type entry. I needed a way to use WordPress’ native “delete post” capability but on a large scale.
This sounds like a job for WP-CLI! Via the command line, we can execute very powerful scripts that would normally timeout or crash.
WARNING: You can do a lot of damage in SSH if you don’t know what you’re doing, so I highly suggest practicing on a demo server first. Then take a full backup of the real website before jumping in on a real project.
The basic steps are:
- Create SSH Keys for your server
- Load your key into the PuTTY Key Generator
- Change key from RSA to DSA using the “Save Private Key” button
- Open Pageant and add your new private key
- Connect to PuTTY using the SSH Credentials provided by your host (hostame, port number, username)
- Navigate to your WP installation using Shell commands (type “ls” to find out where you are and “cd” to move to a new directory)
- Run this command to delete ALL of your “Places” for GeoDirectory (this can’t be undone. there is no “trash” for the custom post type)
wp post delete $(wp post list --post_type='gd_place' --format=ids) --force
And that’s it! If you need step-by-step instructions for setting up PuTTY access, here’s a great tutorial video for Siteground users.
NOTE: When you are pasting your private key into Notepad, make sure to delete the empty line at the bottom that may have accidentally been generated. Leaving it gave me errors when trying to load my key into Pageant.
230,881 is a LOT of records, so this is going to take some time to complete.
I started the command at 6 p.m. on a Saturday and it is running steadily in PuTTY with no timeouts or crashes. I am able to just turn off my screensaver and walk away while it does its thing.
- Update 6:06 p.m. Saturday: 230,881 records is our starting point
- Update 7:46 p.m. Saturday: 202,405 left to go!
- Update 8:38 p.m. Saturday: 187,438 left to go!
- Update 9:08 p.m. Saturday: 178,028 left to go! (Looks like we are averaging about 16k/hour)
- Update 10:40 p.m. Saturday: 144,295 left to go!
- Update 01:51 a.m. Sunday: Not exactly sure when it finished but when I checked just now, they were all deleted!
I wish I had been sitting at my machine to see what happened during the last couple of hours because it appears the process sped up significantly. I assume my host allocated more resources to me automatically so maybe I’ll see it reflected on my bill this month. We shall see!
But the process is done and it was painless. 10/10 would recommend.