โ˜• XOPSSCHOOL โ€” LEARNING MADE AFFORDABLE
Build the Skills Your Next Career Move Demands.
For the Price of One Coffee a Month.
Why spend hundreds on individual courses? Unlock an entire year of technical learning with one Coffee Pass.
US$4.90 /month
US$58.80/year
โ‚น499 + GST/month
โ‚น5,988 + GST/year
โ‚ฌ4.90 /month
โ‚ฌ58.80/year
12 Months of Access • Billed Annually • No Automatic Renewal
DevOps • Cloud • SRE • Kubernetes • Terraform • DevSecOps • AI • AIOps • MLOps • LLMOps • FinOps • GitOps • Platform Engineering & Much More
192
Premium Courses
1,085
Video Sessions
99
Ops Disciplines
86
Tool Courses
340
Assignments
227
Quizzes
โœ“ QR-Verifiable Certificates   โœ“ Learn at Your Own Pace   โœ“ Hands-on Learning   โœ“ New Courses Included
Learn from real-world, instructor-led video sessions by
Rajesh Kumar
Principal Architect • Trainer • Mentor • 15+ Years of Industry Experience
โ˜• Get My Coffee Pass โ€” Start Learning Today →
One Payment. Every Premium Course. One Full Year.
The best investment you can make is in the person you are becoming.

WordPress Commands and Cheatsheet

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_commentmeta and 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

TaskCommand
Show WordPress versionwp core version
Update corewp core update && wp core update-db
Check core files for tamperingwp core verify-checksums
List plugins and their statuswp plugin list
Install and activate a pluginwp plugin install akismet --activate
Update all pluginswp plugin update --all
Deactivate all plugins (troubleshooting)wp plugin deactivate --all
Activate a themewp theme activate twentytwentyfour
List userswp user list
Reset a user passwordwp user update admin --user_pass='NewStrongPass'
Create an admin userwp user create jane jane@example.com --role=administrator
Export / import the databasewp db export file.sql / wp db import file.sql
Run a SQL querywp db query "SELECT COUNT(*) FROM wp_comments;"
Optimize tableswp db optimize
Change domain safely (preview first)wp search-replace 'http://old.com' 'https://new.com' --skip-columns=guid --dry-run
Read the site URLwp option get siteurl
Flush object cache / permalinkswp cache flush / wp rewrite flush
List scheduled cron eventswp cron event list
Toggle maintenance modewp 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

ErrorCauseFix
Table 'db.wp_comments' doesn't existThe site uses a different table prefixCheck with wp db prefix and edit the table names in the queries
Error 1175: You are using safe update modeMySQL Workbench blocks a DELETE whose WHERE clause does not filter on a key columnRun SET SQL_SAFE_UPDATES = 0; for the session, then set it back to 1
YOU ARE RUNNING WP-CLI AS THE ROOT USERWP-CLI refuses to run as root by defaultRun as the web server user: sudo -u www-data wp ...
This does not seem to be a WordPress installationCommand run outside the WordPress directorycd to the WordPress root or add --path=/var/www/html
Posts still show old comment totalscomment_count not recalculated after the SQL deleteRun 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 -p with 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

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services โ€” all in one place.

Explore Hospitals

Related Posts

How to Check a WordPress Website Before Sending Paid Traffic

A marketing team has everything ready for a new Google Ads campaign. The landing page is approved, the ads are scheduled, and the tracking tags are in…

Read More

How to Import DOCX into WordPress with Mammoth: Text, Images, Tables and Code

A practical, single-file publishing workflow for bloggers, technical writers and DevOps training teams. Figure 1. Original featured illustration (1200 ร— 630 pixels). Writing an excellent tutorial is…

Read More

WordPress Typography Patterns: Styled Lists, Notice Boxes, and Code Blocks with Reusable CSS

This guide modernizes a legacy WordPress typography post into practical, reusable patterns for custom list markers, callout boxes, and code styling. It focuses on maintainable CSS and editor-friendly markup you can reuse across posts and documentation pages.

Read More

Moving a WordPress Site to Webflow: What Happens to Your Hosting, DNS and Email

Most guides about Webflow answer one question: is it the right builder for you? This one starts after that decision. You have a WordPress site on Bluehost,…

Read More

How to Choose the Right Website Development and SEO Partner for Your Business

A professional website has evolved far beyond a digital business card or an online brochure. Today, it serves as the core foundation of a brand’s entire online…

Read More

Docker Compose: WordPress + MySQL Using Shared Environment Variables

Create a project folder: Create a .env file: Create docker-compose.yml: Start the containers: Open WordPress: Now students can see how environment variables are passed into both containers….

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
0
Would love your thoughts, please comment.x
()
x