This page shows how to delete WordPress comments in bulk with MySQL and WP-CLI without leaving orphaned data or wrong comment counts. It also includes a WP-CLI cheatsheet for everyday jobs such as backups, plugin updates, user management and search-replace.
Mysql commands to delete wordpress commands in bulk
DELETE FROM `wp_comments` WHERE `wp_comments`.`comment_ID` BETWEEN 1 AND 100000;
Code language: JavaScript (javascript)
What the example above does and what it misses
The query above deletes every row in wp_comments with a comment_ID from 1 to 100000. That works, but it leaves three problems behind, and on a live site you should fix them:
- Orphaned metadata. Comment metadata (Akismet history, ratings, plugin data) is stored in
wp_commentmetaand is not deleted with the comment. - Wrong comment counts. Each post keeps a cached total in
wp_posts.comment_count. Deleting rows directly does not update it, so themes still show the old totals. - Arbitrary selection. An ID range removes approved comments along with spam, and replies outside the range can be left pointing at parents that no longer exist (
comment_parent).
The query also assumes the default wp_ table prefix. Many installations use a different one, set by $table_prefix in wp-config.php.
Back up the database first
Direct SQL deletes cannot be undone. Take a backup before you run any of the commands below.
# With WP-CLI, run from the WordPress root directory
wp db export backup-$(date +%F).sql
# Or with mysqldump
mysqldump -u dbuser -p --single-transaction wordpress_db > backup-$(date +%F).sql
# Find the real table prefix
wp db prefix
grep table_prefix wp-config.phpCode language: PHP (php)
Safer SQL for bulk comment deletion
Select comments by status, not by ID range. The comment_approved column holds 1 (approved), 0 (pending), spam, trash or post-trashed. Run a SELECT first so you can see how many rows a delete will touch. Replace wp_ with your prefix.
-- 1. Preview: how many comments are in each status?
SELECT comment_approved, COUNT(*) FROM wp_comments GROUP BY comment_approved;
-- 2. Delete spam comments and their metadata in one statement
DELETE c, cm
FROM wp_comments c
LEFT JOIN wp_commentmeta cm ON cm.comment_id = c.comment_ID
WHERE c.comment_approved = 'spam';
-- Same pattern for trash or pending: comment_approved IN ('trash', '0')
-- 3. Remove any metadata already orphaned by earlier deletes
DELETE cm
FROM wp_commentmeta cm
LEFT JOIN wp_comments c ON c.comment_ID = cm.comment_id
WHERE c.comment_ID IS NULL;
-- 4. Recalculate the cached comment count on every post
UPDATE wp_posts p
SET p.comment_count = (
SELECT COUNT(*) FROM wp_comments c
WHERE c.comment_post_ID = p.ID AND c.comment_approved = '1'
);Code language: PHP (php)
Step 4 counts only approved comments, which is how WordPress calculates comment_count. To delete every comment on the site, empty both tables and reset the counts:
TRUNCATE TABLE wp_comments;
TRUNCATE TABLE wp_commentmeta;
UPDATE wp_posts SET comment_count = 0;
Note: On very large tables, a single big DELETE can lock wp_comments for a long time. Delete in batches instead, with DELETE FROM wp_comments WHERE comment_approved = 'spam' LIMIT 10000; repeated until it affects 0 rows. Then run the orphan cleanup and recount queries. MySQL does not allow LIMIT on multi-table deletes.
Deleting comments with WP-CLI
WP-CLI goes through the WordPress API, so it deletes comment metadata and updates post counts for you. For most sites this is the better tool. It is slower than raw SQL on very large volumes.
# Count comments by status
wp comment list --status=spam --format=count
wp comment list --status=hold --format=count
# Permanently delete all spam (without --force they go to trash)
wp comment delete $(wp comment list --status=spam --format=ids) --force
# For tens of thousands of IDs, batch them to avoid 'Argument list too long'
wp comment list --status=spam --format=ids | xargs -n 500 wp comment delete --force
# Empty the trash
wp comment list --status=trash --format=ids | xargs -n 500 wp comment delete --force
# Recount comments for all posts after any direct SQL work
wp post list --post_type=any --format=ids | xargs wp comment recount
# Stop new comments on future posts
wp option update default_comment_status closedCode language: PHP (php)
Expected output for each deleted comment is a line like Success: Deleted comment 1234. If no comments match, the subshell returns nothing and WP-CLI prints a usage error. That is harmless.
WP-CLI cheatsheet for everyday tasks
| Task | Command |
|---|---|
| Show WordPress version | wp core version |
| Update core | wp core update && wp core update-db |
| Check core files for tampering | wp core verify-checksums |
| List plugins and their status | wp plugin list |
| Install and activate a plugin | wp plugin install akismet --activate |
| Update all plugins | wp plugin update --all |
| Deactivate all plugins (troubleshooting) | wp plugin deactivate --all |
| Activate a theme | wp theme activate twentytwentyfour |
| List users | wp user list |
| Reset a user password | wp user update admin --user_pass='NewStrongPass' |
| Create an admin user | wp user create jane jane@example.com --role=administrator |
| Export / import the database | wp db export file.sql / wp db import file.sql |
| Run a SQL query | wp db query "SELECT COUNT(*) FROM wp_comments;" |
| Optimize tables | wp db optimize |
| Change domain safely (preview first) | wp search-replace 'http://old.com' 'https://new.com' --skip-columns=guid --dry-run |
| Read the site URL | wp option get siteurl |
| Flush object cache / permalinks | wp cache flush / wp rewrite flush |
| List scheduled cron events | wp cron event list |
| Toggle maintenance mode | wp maintenance-mode activate / wp maintenance-mode deactivate |
Note: Use wp search-replace instead of a SQL REPLACE() when you change URLs. WordPress stores many options as serialized PHP. WP-CLI fixes the string lengths inside that data, and plain SQL breaks them.
Common errors and fixes
| Error | Cause | Fix |
|---|---|---|
Table 'db.wp_comments' doesn't exist | The site uses a different table prefix | Check with wp db prefix and edit the table names in the queries |
Error 1175: You are using safe update mode | MySQL Workbench blocks a DELETE whose WHERE clause does not filter on a key column | Run SET SQL_SAFE_UPDATES = 0; for the session, then set it back to 1 |
YOU ARE RUNNING WP-CLI AS THE ROOT USER | WP-CLI refuses to run as root by default | Run as the web server user: sudo -u www-data wp ... |
This does not seem to be a WordPress installation | Command run outside the WordPress directory | cd to the WordPress root or add --path=/var/www/html |
| Posts still show old comment totals | comment_count not recalculated after the SQL delete | Run the UPDATE wp_posts recount query or wp comment recount |
Best practices
- Test bulk deletes on a staging copy, and always keep a fresh backup.
- Before you delete anything, run the same WHERE clause as a
SELECT COUNT(*). - Prefer WP-CLI for routine cleanup. Use raw SQL only for very large volumes, then clean up orphans and recount.
- Stop spam at the source with an anti-spam plugin, comment moderation, or by closing comments on old posts (
wp option update close_comments_for_old_posts 1). - Never paste database passwords on the command line on shared servers. Use
-pwith no value so mysqldump prompts for it, or use an option file.
Corrections to the original
The heading above says "delete wordpress commands in bulk". It should say comments: the query removes rows from wp_comments. The query itself is valid SQL, but it is incomplete. Use the status-based delete, the orphaned-metadata cleanup and the comment_count recount from the safer SQL section to get a clean result.
Frequently asked questions
How do I delete all spam comments in WordPress at once?
Run wp comment delete $(wp comment list --status=spam --format=ids) --force from the WordPress root. Without WP-CLI, use the SQL multi-table DELETE on comment_approved = 'spam', then recount comment_count.
Does deleting rows from wp_comments also delete wp_commentmeta?
No. A direct SQL delete on wp_comments leaves the matching wp_commentmeta rows behind. Delete both tables together with a JOIN, or clean up orphans afterwards. WP-CLI and the admin dashboard remove metadata automatically.
Why do my posts still show the old comment count after deleting comments?
WordPress caches the total in wp_posts.comment_count, and direct SQL does not update it. Run the recount UPDATE query or wp comment recount for the affected posts.
Is it safe to TRUNCATE wp_comments?
Yes, if you really want to remove every comment. Back up first, truncate wp_commentmeta as well, and set comment_count to 0 in wp_posts.
How do I find my WordPress table prefix?
Run wp db prefix, or look for the $table_prefix line in wp-config.php. Replace wp_ in every query with that value.
Related tutorials
- More Web Development tutorials
- Best Tools for Writing Official Product Documentation in HTML: A Complete 2026 Guide
I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms.
I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services โ all in one place.
Explore Hospitals