WP-CLI is nothing new to most of the wordpress users or developers. It saves an enormous amount of time when it comes to installing, updating, deleting wordpress updates, plugins, themes, comments – and a lot of other stuffs. If you haven’t been using wp-cli, you have wasted a lof of productive hours in your life. In that case, get a grasp of what wp-cli is.
As we have said it does a hell lot of things, today we’re going to see how we can bulk delete wordpress comments that we no longer wish to be stored in the wordpress database. If you have a high traffic site or a popular blog or a network poster from your blog (for promotion), you might be getting a lot of pingbacks or trackbacks – which will be stored as comments in your wordpress. Now many a times we might not simply want to have them stored and keep them on hold. So we’d need to delete them in bulk. Using wordpress’s UI with paging can be tedious deleting hundreds of thousands of comments. So we need a shortcut – which saves our precious time.
Before we begin to delete the comments, let’s see how many statuses a comment might have in wordpress. They are :
Now as for the first one “hold”, it might be listed as “Pending” in your comments panel in wordpress. Say we have 577 “pending” comments that we want to clean. wp-cli by default doesn’t give a single command which will delete all the comments with criteria. It can list them and delete them separately.
To delete the comments we’d need the IDs of the comments. So first, we list them and then with the IDs we delete them. Actually we delete them passing the list as parameters 🙂
To list the comments which are “pending”
wp comment list --status=hold --field=ID
This will give us the IDs one by one per line in the command line output.
Now to delete them with IDs we normally use
wp comment delete ID1 ID2 ID2
Now the magic
wp comment delete $(wp comment list --status=hold --field=ID)
This would delete all the comments automatically which are “pending’. You might get the command line output like
Success: Deleted comment 36. Success: Deleted comment 35. Success: Deleted comment 34. Success: Deleted comment 33. Success: Deleted comment 32. Success: Deleted comment 31. Success: Deleted comment 30. Success: Deleted comment 29. Success: Deleted comment 28. Success: Deleted comment 27. Success: Deleted comment 26. Success: Deleted comment 25. Success: Deleted comment 22. Success: Deleted comment 21.
And, if you want to clean the trash as well, just change the status of the last command from “hold” to “trash”.
wp comment delete $(wp comment list --status=trash --field=ID)
Alrightee, we have saved some minutes (I guess 😉 )