Web Hosting Blog by Nest Nepal | Domain & Hosting Tips

Using WP-CLI for Bulk Site Maintenance and Updates

Managing multiple WordPress sites can quickly become a nightmare when you’re clicking through dashboards, manually updating plugins, and running the same maintenance tasks over and over. WP-CLI changes that game entirely by letting you automate bulk operations across dozens or even hundreds of sites from a single command line.

Whether you’re managing a small agency portfolio or enterprise-level multisite networks, WP-CLI transforms tedious manual work into efficient, scriptable commands that save hours of repetitive clicking.

What Makes WP-CLI Perfect for Bulk Operations

wp-cli

WP-CLI isn’t just a convenience tool; it’s built specifically for scenarios where the WordPress admin interface falls short. When you need to update 50 sites, check plugin versions across your entire network, or run database cleanups on multiple installations, the web interface becomes a bottleneck.

The real power comes from WP-CLI’s ability to:

  • Execute identical commands across multiple sites instantly
  • Generate detailed reports on site status and health
  • Automate routine maintenance that would otherwise require manual intervention
  • Handle operations that would time out or fail in browser-based environments

Essential Setup for Multi-Site Management

Before diving into bulk operations, you need WP-CLI to be properly configured for your environment. Most hosting providers now include WP-CLI by default, but if you’re on a VPS or dedicated server, installation is straightforward:

# Download and install WP-CLI

curl -O https://raw.githubusercontent.com/wp-cli/wp-cli/v2.8.1/phar/wp-cli.phar

chmod +x wp-cli.phar

sudo mv wp-cli.phar /usr/local/bin/wp

# Verify installation

wp –info

For bulk operations, organize your sites in a consistent directory structure. This makes scripting much easier:

/var/www/

├── client1-site.com/

├── client2-blog.com/

├── agency-portfolio.com/

└── ecommerce-store.com/

Core Bulk Update Commands

Plugin Management Across Multiple Sites

Keeping plugins updated is probably the most common bulk operation. Here’s how to handle it efficiently:

# Check plugin status across all sites

for dir in /var/www/*/; do

    echo “Checking: $(basename “$dir”)”

    wp –path=”$dir” plugin list –update=available

done

# Update all plugins on all sites

for dir in /var/www/*/; do

    echo “Updating plugins for: $(basename “$dir”)”

    wp –path=”$dir” plugin update –all

done

# Update specific plugin across all sites

for dir in /var/www/*/; do

    wp –path=”$dir” plugin update woocommerce

done

WordPress Core Updates

Core updates require a bit more caution, but WP-CLI makes them manageable:

# Check WordPress version across all sites

for dir in /var/www/*/; do

    site=$(basename “$dir”)

    version=$(wp –path=”$dir” core version)

    echo “$site: $version”

done

# Download core updates (without installing)

for dir in /var/www/*/; do

    wp –path=”$dir” core download –version=latest –force

done

# Update WordPress core with backup

for dir in /var/www/*/; do

    echo “Updating core for: $(basename “$dir”)”

    wp –path=”$dir” db export “backup-$(date +%Y%m%d).sql”

    wp –path=”$dir” core update

done

Theme Management

Theme updates follow similar patterns but often require more selective handling:

# List all active themes

for dir in /var/www/*/; do

    site=$(basename “$dir”)

    theme=$(wp –path=”$dir” theme list –status=active –field=name)

    echo “$site: $theme”

done

# Update all themes

for dir in /var/www/*/; do

    wp –path=”$dir” theme update –all

done

Database Maintenance Operations

Database maintenance is where WP-CLI shines for bulk operations. These tasks would be incredibly time-consuming through the admin interface:

Database Optimization

# Optimize databases across all sites

for dir in /var/www/*/; do

    echo “Optimizing database for: $(basename “$dir”)”

    wp –path=”$dir” db optimize

done

# Clean up spam comments, revisions, and transients

for dir in /var/www/*/; do

    wp –path=”$dir” comment delete $(wp –path=”$dir” comment list –status=spam –format=ids)

    wp –path=”$dir” post delete $(wp –path=”$dir” post list –post_status=trash –format=ids)

    wp –path=”$dir” transient delete –all

done

Database Repairs and Checks

# Check database integrity

for dir in /var/www/*/; do

    echo “Checking: $(basename “$dir”)”

    wp –path=”$dir” db check

done

# Repair databases if needed

for dir in /var/www/*/; do

    wp –path=”$dir” db repair

done

User Management Across Sites

Managing users becomes much simpler with WP-CLI, especially when you need to make changes across multiple sites:

# Create admin user across all sites

for dir in /var/www/*/; do

    wp –path=”$dir” user create agency_admin admin@agency.com \

        –role=administrator –user_pass=secure_password

done

# Update user passwords

for dir in /var/www/*/; do

    wp –path=”$dir” user update admin –user_pass=new_secure_password

done

# List all administrator users

for dir in /var/www/*/; do

    site=$(basename “$dir”)

    echo “Admins for $site:”

    wp –path=”$dir” user list –role=administrator –field=user_email

done

Advanced Bulk Operations

Media Library Maintenance

# Regenerate thumbnails across all sites

for dir in /var/www/*/; do

    echo “Regenerating thumbnails for: $(basename “$dir”)”

    wp –path=”$dir” media regenerate –yes

done

# Clean up unused media files

for dir in /var/www/*/; do

    wp –path=”$dir” media import –featured_image

done

Search and Replace Operations

This is particularly useful when moving sites or updating URLs:

# Update URLs across multiple sites

for dir in /var/www/*/; do

    wp –path=”$dir” search-replace ‘http://olddomian.com’ ‘https://newdomain.com’

done

# Update email addresses

for dir in /var/www/*/; do

    wp –path=”$dir” search-replace ‘old@email.com’ ‘new@email.com’

done

Creating Comprehensive Maintenance Scripts

For regular maintenance, create shell scripts that combine multiple operations:

#!/bin/bash

# bulk-maintenance.sh

SITES_DIR=”/var/www”

LOG_FILE=”/var/log/wp-maintenance-$(date +%Y%m%d).log”

echo “Starting bulk maintenance: $(date)” >> $LOG_FILE

for dir in $SITES_DIR/*/; do

    if [ -f “$dir/wp-config.php” ]; then

        site=$(basename “$dir”)

        echo “Processing: $site” >> $LOG_FILE

        # Create backup

        wp –path=”$dir” db export “backup-$site-$(date +%Y%m%d).sql”

        # Update everything

        wp –path=”$dir” core update >> $LOG_FILE 2>&1

        wp –path=”$dir” plugin update –all >> $LOG_FILE 2>&1

        wp –path=”$dir” theme update –all >> $LOG_FILE 2>&1

        # Clean up

        wp –path=”$dir” transient delete –all

        wp –path=”$dir” db optimize

        echo “Completed: $site” >> $LOG_FILE

    fi

done

echo “Bulk maintenance completed: $(date)” >> $LOG_FILE

Monitoring and Reporting

Generate comprehensive reports on your site network:

# Site health report

echo “Site Health Report – $(date)”

echo “==================================”

for dir in /var/www/*/; do

    site=$(basename “$dir”)

    wp_version=$(wp –path=”$dir” core version)

    active_plugins=$(wp –path=”$dir” plugin list –status=active –format=count)

    echo “$site | WP: $wp_version | Plugins: $active_plugins”

done

OperationSingle Site TimeBulk Time (10 sites)Time Saved
Plugin Updates5 minutes2 minutes48 minutes
Core Updates3 minutes1 minute29 minutes
Database Cleanup10 minutes3 minutes97 minutes

Security Considerations

When running bulk operations, security becomes even more critical:

  • Always create database backups before major updates
  • Use strong passwords when creating bulk users
  • Test scripts on staging environments first
  • Monitor logs for failed operations
  • Implement proper file permissions on your scripts

# Secure your maintenance scripts

chmod 700 bulk-maintenance.sh

chown root:root bulk-maintenance.sh

Troubleshooting Common Issues

Memory Limits: Large bulk operations can hit PHP memory limits. Increase limits in wp-config.php:

ini_set(‘memory_limit’, ‘512M’);

Timeouts: For operations on many sites, break them into smaller batches:

# Process sites in batches of 5

sites=($(find /var/www -name “wp-config.php” -path “*/public_html/*” | head -5))

Permission Issues: Ensure WP-CLI runs with appropriate permissions:

# Run as web server user

sudo -u www-data wp plugin update –all

WP-CLI transforms WordPress maintenance from a tedious manual process into an efficient, automated workflow. Whether you’re managing 5 sites or 500, these bulk operations will save you countless hours while ensuring consistent maintenance across your entire network. The initial time investment in setting up proper scripts pays dividends every single maintenance cycle.

Share this article
Shareable URL
Prev Post

Choosing the Right WordPress Page Builder (Divi vs Elementor vs Gutenberg) for Nepali Sites

Next Post

Setting Up a WordPress Staging Environment in cPanel Without Extra Plugins

Leave a Reply

Your email address will not be published. Required fields are marked *

Read next