WP-CLI is the command-line interface for WordPress that enables powerful administration and optimization tasks impossible through the web interface. For performance optimization, WP-CLI provides direct access to database operations, cache management, and bulk operations that would be impractical through the admin dashboard. This guide covers using WP-CLI for systematic WordPress performance improvement.
Installing WP-CLI
# Download and install
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp
# Verify
wp --info
# Enable tab completion
echo 'source /usr/share/bash-completion/completions/wp' >> ~/.bashrc
Database Optimization
Clean Post Revisions
# Check revision count
wp post list --post_type=revision --format=count
# Often thousands of revisions accumulating
# Delete all revisions
wp post delete $(wp post list --post_type=revision --field=ID) --force
# Keep only last 5 revisions per post
# Add to wp-config.php:
# define('WP_POST_REVISIONS', 5);
# Or delete old revisions via SQL (faster for large databases)
wp db query "DELETE FROM wp_posts WHERE post_type = 'revision' AND post_date < DATE_SUB(NOW(), INTERVAL 30 DAY)"
Clean Transients
# List expired transients count
wp transient list --expired --format=count
# Delete all expired transients
wp transient delete --expired
# Delete ALL transients (they'll regenerate)
wp transient delete --all
# Check database size before/after
wp db size --tables --format=table
Optimize Database Tables
# Optimize all WordPress tables
wp db optimize
# Repair if needed
wp db repair
# Check table sizes
wp db size --tables --format=table --order=size --orderby=desc
# Clean orphaned metadata
wp db query "DELETE pm FROM wp_postmeta pm LEFT JOIN wp_posts p ON pm.post_id = p.ID WHERE p.ID IS NULL"
wp db query "DELETE cm FROM wp_commentmeta cm LEFT JOIN wp_comments c ON cm.comment_id = c.comment_ID WHERE c.comment_ID IS NULL"
# Clean auto-drafts
wp post delete $(wp post list --post_status=auto-draft --field=ID) --force 2>/dev/null
Cache Management
# Flush object cache
wp cache flush
# Flush rewrite rules (fixes permalink issues)
wp rewrite flush
# Clear all caches including page cache plugins
wp cache flush
wp w3-total-cache flush all 2>/dev/null # W3 Total Cache
wp litespeed-purge all 2>/dev/null # LiteSpeed Cache
wp wp-super-cache flush 2>/dev/null # WP Super Cache
Plugin and Theme Audit
# List all plugins with status
wp plugin list --format=table
# Find inactive plugins (remove for security)
wp plugin list --status=inactive --format=table
# Deactivate and delete unused plugins
wp plugin deactivate plugin-name
wp plugin delete plugin-name
# Check plugin performance impact
# Install Query Monitor
wp plugin install query-monitor --activate
# List themes (remove unused)
wp theme list --format=table
wp theme delete twentytwentytwo twentytwentythree
# Update all plugins (keep updated for performance patches)
wp plugin update --all
# Check for plugins with known performance issues
wp plugin list --fields=name,status,update --format=csv | while IFS=, read name status update; do
echo "$name: $status (update: $update)"
done
Image Optimization
# Regenerate thumbnails (after changing theme or image sizes)
wp media regenerate --yes
# Only regenerate missing sizes
wp media regenerate --only-missing --yes
# Check for large unoptimized images
wp db query "SELECT ID, guid, ROUND(LENGTH(post_content)/1024) as kb FROM wp_posts WHERE post_type='attachment' AND post_mime_type LIKE 'image/%' ORDER BY LENGTH(post_content) DESC LIMIT 20"
# Import images and generate all sizes
wp media import /path/to/images/*.jpg
Performance Configuration
# Set performance-related wp-config constants
wp config set WP_POST_REVISIONS 5 --raw
wp config set AUTOSAVE_INTERVAL 300 --raw # 5 minutes instead of 60s
wp config set WP_MEMORY_LIMIT '256M'
wp config set WP_MAX_MEMORY_LIMIT '512M'
wp config set COMPRESS_CSS true --raw
wp config set COMPRESS_SCRIPTS true --raw
wp config set CONCATENATE_SCRIPTS true --raw
# Disable wp-cron (use system cron instead)
wp config set DISABLE_WP_CRON true --raw
# Add system cron
# */5 * * * * cd /var/www/wordpress && wp cron event run --due-now --quiet
Search and Replace (Migration Performance)
# Dry run search-replace (for domain migration)
wp search-replace 'old-domain.com' 'new-domain.com' --dry-run --precise
# Execute with correct serialization handling
wp search-replace 'http://old-domain.com' 'https://new-domain.com' \
--precise --recurse-objects --all-tables
# This is 10-100x faster than doing it through PHP/plugins
Automated Maintenance Script
#!/bin/bash
# /usr/local/bin/wp-optimize.sh
# Run weekly: 0 3 * * 0 /usr/local/bin/wp-optimize.sh
WP_PATH="/var/www/wordpress"
cd "$WP_PATH"
echo "=== WordPress Optimization $(date) ==="
# Clean revisions older than 30 days
REVISIONS=$(wp post list --post_type=revision --format=count)
wp db query "DELETE FROM wp_posts WHERE post_type = 'revision' AND post_date < DATE_SUB(NOW(), INTERVAL 30 DAY)"
echo "Cleaned revisions (had $REVISIONS)"
# Clean transients
TRANSIENTS=$(wp transient list --expired --format=count)
wp transient delete --expired
echo "Cleaned $TRANSIENTS expired transients"
# Clean auto-drafts
wp post delete $(wp post list --post_status=auto-draft --field=ID 2>/dev/null) --force 2>/dev/null
# Clean orphaned data
wp db query "DELETE FROM wp_postmeta WHERE post_id NOT IN (SELECT ID FROM wp_posts)"
wp db query "DELETE FROM wp_commentmeta WHERE comment_id NOT IN (SELECT comment_ID FROM wp_comments)"
wp db query "DELETE FROM wp_comments WHERE comment_approved = 'spam'"
# Optimize tables
wp db optimize
# Report database size
echo "Database size:"
wp db size --tables --format=table | head -20
# Update plugins and themes
wp plugin update --all
wp theme update --all
wp core update
echo "=== Optimization complete ==="
Profiling with WP-CLI
# Install profiling package
wp package install wp-cli/profile-command
# Profile a page load
wp profile hook --url=https://example.com/ --fields=callback,cache_hits,cache_misses,time
# Profile specific hooks
wp profile hook template_redirect --url=https://example.com/
# Find slow queries
wp profile hook --url=https://example.com/ --spotlight --fields=callback,time,query_count
Summary
WP-CLI transforms WordPress maintenance from manual dashboard clicks into automated, scriptable operations. The biggest performance wins come from database cleanup (revisions, transients, orphaned metadata), proper cron configuration (system cron instead of wp-cron), plugin auditing (removing unused plugins), and OPcache/cache management. Set up the automated maintenance script to run weekly, and use the profiling commands to identify which plugins and hooks are the biggest performance bottlenecks.