If you only have a few spam comments, deleting them through the admin panel is quick. But if your website has thousands or even tens of thousands of spam comments, like in the example image, the following method will help you delete them quickly and easily.
In this guide, I’ll use WP CLI to delete comments and execute the commands in the terminal.
- If you are using a VPS, log in via SSH to proceed.
- If you are using hosting, your hosting provider must support terminal access for this to work. In Vietnam, I’ve noticed that providers like Azdigi and iNet offer terminal access in some of their premium plans.
Step 1: Open the terminal
On cPanel hosting, you will find the terminal interface as shown in the image below.
Step 2: Navigate to the website’s root directory
Identify the root directory of the website where you want to delete spam comments, then type the following command and press Enter:
For example, if your website is located in the public_html
folder, you would type:
cd public_html
Step 3: The key step – Enter the command to delete spam comments
wp comment delete $(wp comment list --status=hold --field=ID --allow-root) --force --allow-root
In this command:
--status=hold
can be replaced with other statuses likeapproved
(approved),spam
(marked as spam),trash
(moved to trash), orhold
(pending approval).
After hitting Enter, the system will delete all spam comments. Deleting 3,000 spam comments only takes 1-2 seconds. Pretty fast, right?