WP-CLI: Managing WordPress from the Command Line
Published: March 30, 2026 · Author: Marcin Szewczyk-Wilgan
The WordPress admin panel works fine when you manage a single site. With multiple sites, staging environments, regular updates, and cron tasks – clicking through the interface becomes a bottleneck. WP-CLI is the official command-line tool for managing WordPress. It lets you do everything from the terminal that you do in the admin panel – and much more: bulk plugin updates, database export and import, user management, transient cleanup, cron management, post-migration search-replace, and automation of repetitive tasks in bash scripts. If you are looking for lower-level commands – grep, find, sed, awk – check out the article WordPress on the Server Terminal. In this article, you will find practical WP-CLI commands grouped by topic – from installation and configuration, through daily maintenance, to advanced database operations and automation.
1. Installation and configuration of WP-CLI
WP-CLI requires PHP 5.6+ and terminal access on the server. Most good hosting providers (VPS, dedicated, quality shared) have WP-CLI pre-installed. If not – installation takes a minute. Need help with environment setup? We handle server administration and WordPress hosting.
Install WP-CLI:
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/wpCheck version and environment:
wp --version
wp --infoUpdate WP-CLI to the latest version:
wp cli updateCommand auto-completion (bash completion):
wp cli completions --type=bash >> ~/.bashrc
source ~/.bashrcRun as a different user – important on servers with multiple sites:
sudo -u www-data wp option get siteurl --path=/var/www/html2. WordPress installation and management
WP-CLI lets you install WordPress from scratch – downloading files, creating wp-config.php, installing the database – without opening a browser. Useful for deployment automation and server provisioning.
Download WordPress files:
wp core download --locale=en_USCreate wp-config.php:
wp config create --dbname=wordpress --dbuser=root --dbpass=password --dbhost=localhost --dbprefix=wp_Install WordPress – create database and admin account:
wp core install --url="https://domain.com" --title="My Site" --admin_user=admin --admin_email=admin@domain.com --admin_password=strongpasswordCheck the current WordPress version:
wp core versionUpdate WordPress to the latest version:
wp core update
wp core update-dbToggle debug mode:
wp config set WP_DEBUG true --raw
wp config set WP_DEBUG_LOG true --rawDisable debug mode after diagnostics:
wp config set WP_DEBUG false --raw3. Plugins and themes
Managing plugins and themes with WP-CLI is a clear advantage over the admin panel – especially for bulk updates, audits, and scripts automating maintenance of multiple sites. We discuss the criteria for choosing a WordPress theme in a separate article.
List installed plugins:
wp plugin listList plugins requiring updates:
wp plugin list --update=availableUpdate all plugins:
wp plugin update --allInstall and activate a plugin:
wp plugin install wordfence --activateDeactivate a plugin – e.g., after an error:
wp plugin deactivate wordfenceDelete inactive plugins:
wp plugin delete $(wp plugin list --status=inactive --field=name)List themes:
wp theme listActivate a theme:
wp theme activate flavorUpdate all themes:
wp theme update --all4. Database
The database is the heart of WordPress. WP-CLI provides tools for export, import, optimization, cleanup, and search-replace operations – the latter cannot be safely performed with other tools because WP-CLI correctly handles PHP serialized data. More on MySQL administration and PostgreSQL on the service pages.
Export the database (SQL dump):
wp db export backup.sqlImport a database:
wp db import backup.sqlOptimize tables – equivalent to OPTIMIZE TABLE:
wp db optimizeRepair damaged tables:
wp db repairSearch-replace after migration – safely handles serialized data:
wp search-replace 'https://old-domain.com' 'https://new-domain.com' --all-tables --dry-runRemove --dry-run after verification to perform the actual replacement.
Search-replace with per-table change report:
wp search-replace 'old-domain.com' 'new-domain.com' --all-tables --report-changed-onlyExecute an SQL query directly:
wp db query "SELECT COUNT(*) FROM wp_posts WHERE post_type='revision'"Delete post revisions – saves space:
wp post delete $(wp post list --post_type=revision --format=ids) --forceDelete expired transients:
wp transient delete --expiredDelete ALL transients:
wp transient delete --allCount autoload records in wp_options:
wp db query "SELECT COUNT(*) FROM wp_options WHERE autoload='yes'"Autoload size – a common cause of slow TTFB:
wp db query "SELECT SUM(LENGTH(option_value)) as autoload_size FROM wp_options WHERE autoload='yes'"5. Users and permissions
Managing users with WP-CLI is faster than through the admin panel – especially for bulk operations, permission audits, and password resets after security incidents.
List users:
wp user listCreate a new user:
wp user create john john@domain.com --role=editor --display_name="John Smith"Change a user's password:
wp user update admin --user_pass=newstrongpasswordChange a user's role:
wp user set-role john administratorDelete a user and reassign their content to another:
wp user delete john --reassign=1List administrators – security audit:
wp user list --role=administrator --fields=ID,user_login,user_emailForce log out all users – after a security incident:
wp user session destroy --all6. Cron and scheduled tasks
WordPress has its own cron system (wp-cron) that runs scheduled tasks on page visits. On low-traffic sites, tasks can be delayed, and on high-traffic sites, wp-cron generates unnecessary load. WP-CLI lets you manage cron manually and move it to the system crontab.
List scheduled cron events:
wp cron event listRun a specific event manually:
wp cron event run wp_update_pluginsRun all overdue events:
wp cron event run --due-nowDisable wp-cron and switch to system crontab:
wp config set DISABLE_WP_CRON true --rawThen add to the server's crontab:
*/15 * * * * cd /var/www/html && wp cron event run --due-now --quietDelete an orphaned cron event – e.g., after removing a plugin:
wp cron event delete event_nameCheck if wp-cron is disabled:
wp config get DISABLE_WP_CRON7. Content and media
WP-CLI lets you manage posts, pages, comments, and the media library without opening the admin panel. Useful for bulk operations – deleting spam, regenerating thumbnails after a theme change, exporting content. Need a custom solution for content management? We are happy to help.
List posts:
wp post list --post_type=post --fields=ID,post_title,post_statusList pages with draft status:
wp post list --post_type=page --post_status=draftCreate a new post:
wp post create --post_type=post --post_title="New post" --post_status=publishDelete spam comments:
wp comment delete $(wp comment list --status=spam --format=ids) --forceCount comments per status:
wp comment list --format=count --status=approved
wp comment list --format=count --status=spam
wp comment list --format=count --status=trashRegenerate thumbnails – after a theme change or size adjustments:
wp media regenerate --yesRegenerate thumbnails for a specific size only:
wp media regenerate --image_size=thumbnail --yesImport images from a directory:
wp media import /path/to/images/*8. Options and configuration
The wp_options table stores the configuration of WordPress, plugins, and the theme. WP-CLI lets you read, modify, and delete options without logging into the database.
Check site URL:
wp option get siteurl
wp option get homeChange site URL – e.g., after migration:
wp option update siteurl 'https://new-domain.com'
wp option update home 'https://new-domain.com'Check the active theme:
wp option get template
wp option get stylesheetDisable comments globally:
wp option update default_comment_status closedCheck the value of any option:
wp option get blogdescriptionList autoload options – diagnosing slow TTFB:
wp option list --autoload=on --fields=option_name,option_value --format=table | head -30Delete orphaned options from a plugin:
wp option delete plugin_option_name9. Cache and performance
WP-CLI gives you control over object cache, transients, and plugin caches directly from the terminal – without logging into the admin panel. More on caching strategies in the article Core Web Vitals Optimization and on the WordPress optimization service page.
Flush object cache:
wp cache flushDelete expired transients:
wp transient delete --expiredCheck object cache type – Redis, Memcached, file-based:
wp cache typeFlush WP Super Cache:
wp super-cache flushFlush WP Rocket cache:
wp rocket clean --confirmRun WP Rocket preload cache:
wp rocket preload10. Automation and scripts
The true power of WP-CLI shows in bash scripts. You can automate complete site maintenance – updates, backup, cleanup, monitoring – and run it via cron or manually with a single command. Don't want to handle this yourself? As part of our WordPress care plans, we do it for you.
Script: backup + update + cleanup:
#!/bin/bash
cd /var/www/html
# Database backup
wp db export /backup/db-$(date +%Y%m%d).sql
# Update core, plugins, themes
wp core update
wp core update-db
wp plugin update --all
wp theme update --all
# Cleanup
wp transient delete --expired
wp cache flush
echo "Maintenance complete: $(date)"Managing multiple sites in a loop:
for site in /var/www/site1 /var/www/site2 /var/www/site3; do
echo "=== $site ==="
wp plugin update --all --path="$site"
wp core update --path="$site"
doneSite status report – versions, plugins, updates:
echo "WordPress: $(wp core version)"
echo "PHP: $(php -v | head -1)"
echo "Plugins to update:"
wp plugin list --update=available --fields=name,version,update_versionDaily cron with backup and updates:
0 3 * * * cd /var/www/html && bash /scripts/wp-maintenance.sh >> /var/log/wp-maintenance.log 2>&1Export plugin list to CSV – for documentation:
wp plugin list --format=csv > plugins-$(date +%Y%m%d).csvFrequently Asked Questions
Yes – WP-CLI is an official tool, developed by the WordPress community and supported by Automattic. It is the standard in professional WordPress administration. The only risk comes from user errors – that is why for destructive operations (e.g., search-replace, deleting posts) you should use the --dry-run flag before actual execution.
It depends on the hosting provider. Most quality WordPress hosts have WP-CLI pre-installed. On cheap shared plans it may be unavailable – you need SSH access and PHP CLI. If your hosting does not provide WP-CLI, consider switching to a better provider.
WP-CLI search-replace correctly handles PHP serialized data – it updates both the text and the string length counters in serialization. Sed does not do this and will corrupt serialized data. For domain replacement in the WordPress database, always use WP-CLI, not sed.
Use the --path=/path/to/site flag with each command, e.g., wp plugin list --path=/var/www/site2. For WordPress Multisite, add --url=subdomain.domain.com to target a specific site in the network.
WP-CLI does not have a built-in undo. That is why before destructive operations you should always create a backup: wp db export backup.sql before search-replace, --dry-run before bulk changes. For updates – a staging environment lets you test changes before production.


