Clean up WordPress autoload

last updated 21 hours ago

The problem: Too many autoloaded options

WordPress loads all options with each page load autoload='yes' from the database. With 546 autoloaded options with a total size of 960 KB, each request is unnecessarily slowed down.

Attention: WordPress does not clean up autoload options by itself – that means you have to do it yourself!

Here I show how to solve the problem.

Conduct analysis

Automatically extract MySQL credentials

# SSH connection ssh user@server # To WordPress directory cd /home/domain.com/public_html # Extract credentials from wp-config.php DB_NAME=$(grep DB_NAME wp-config.php | cut -d "'" -f 4) DB_USER=$(grep DB_USER wp-config.php | cut -d "'" -f 4) DB_PASS=$(grep DB_PASSWORD wp-config.php | cut -d "'" -f 4) DB_HOST=$(grep DB_HOST wp-config.php | cut -d "'" -f 4 | cut -d':' -f1) PREFIX=$(grep table_prefix wp-config.php | cut -d "'" -f 2)

Check status

mysql -u$DB_USER -p$DB_PASS -h$DB_HOST $DB_NAME -e " SELECT COUNT(*) as 'Number of Options', ROUND(SUM(LENGTH(option_value))/1024,2) as 'Size (KB)' FROM ${PREFIX}options WHERE autoload='yes';"

Perform cleanup

1. Create a backup (critical!)

mysqldump -u$DB_USER -p$DB_PASS -h$DB_HOST $DB_NAME ${PREFIX}options > \ /tmp/options_backup_$(date +%Y%m%d_%H%M%S).sql

2. Clean up transients

Transients should never be autoloaded:

mysql -u$DB_USER -p$DB_PASS -h$DB_HOST $DB_NAME -e " UPDATE ${PREFIX}options SET autoload='no' WHERE option_name LIKE '%_transient_%' AND autoload='yes'; DELETE FROM ${PREFIX}options WHERE option_name LIKE '%_transient_timeout_%' AND option_value < UNIX_TIMESTAMP();"

3. Plugin-specific cleanup

mysql -u$DB_USER -p$DB_PASS -h$DB_HOST $DB_NAME << EOF -- Logs and debug data UPDATE ${PREFIX}options SET autoload='no' WHERE option_name LIKE '%_log%' OR option_name LIKE '%_debug_%'; -- cache entries UPDATE ${PREFIX}options SET autoload='no' WHERE option_name LIKE '%_cache_%'; -- Elementor assets (regenerated when needed) UPDATE ${PREFIX}options SET autoload='no' WHERE option_name IN ('_elementor_assets_data', 'elementor-custom-breakpoints-files'); -- WooCommerce Payment Gateways (only needed at checkout) UPDATE ${PREFIX}options SET autoload='no' WHERE option_name LIKE 'woocommerce_stripe_%_settings' OR option_name LIKE 'woocommerce_fkwcs_%'; -- Cron (can be set to 'no') UPDATE ${PREFIX}options SET autoload='no' WHERE option_name = 'cron'; EOF

4. Deactivate empty options

mysql -u$DB_USER -p$DB_PASS -h$DB_HOST $DB_NAME -e " UPDATE ${PREFIX}options SET autoload='no' WHERE autoload='yes' AND LENGTH(option_value) = 0 AND option_name NOT IN ('WPLANG','upload_path','category_base','tag_base');"

Set up automatic maintenance

Create maintenance script

# Scripts directory outside web root mkdir -p /home/domain.com/scripts chmod 750 /home/domain.com/scripts cat > /home/domain.com/scripts/autoload_maintenance.sh << 'EOF' #!/bin/bash WP_PATH="/home/domain.com/public_html" cd $WP_PATH # Extract DB Credentials DB_NAME=$(grep DB_NAME wp-config.php | cut -d "'" -f 4) DB_USER=$(grep DB_USER wp-config.php | cut -d "'" -f 4) DB_PASS=$(grep DB_PASSWORD wp-config.php | cut -d "'" -f 4) DB_HOST=$(grep DB_HOST wp-config.php | cut -d "'" -f 4 | cut -d':' -f1) PREFIX=$(grep table_prefix wp-config.php | cut -d "'" -f 2) # cleanup mysql -u$DB_USER -p$DB_PASS -h$DB_HOST $DB_NAME -e " DELETE FROM ${PREFIX}options WHERE option_name LIKE '%_transient_%' AND autoload='yes'; UPDATE ${PREFIX}options SET autoload='no' WHERE option_name LIKE '%_log%' AND autoload='yes';" # Clear cache rm -rf $WP_PATH/wp-content/cache/* rm -rf $WP_PATH/wp-content/litespeed/* EOF chmod +x /home/domain.com/scripts/autoload_maintenance.sh

Set up a cron job

# Weekly execution (crontab -l 2>/dev/null; echo "0 4 * * 0 /home/domain.com/scripts/autoload_maintenance.sh") | crontab -

Results

In my practical test I was able to use the autoloaded options from:

  • 546 Options (960 KB) on
  • 208 Options (3.07 KB) reduce

This corresponds to a 98% Reduction of the amount of data.

Important instructions

Security

  • Never save scripts in the public_html directory
  • Always create a backup before making changes
  • In case of errors: mysql -u$DB_USER -p$DB_PASS -h$DB_HOST $DB_NAME < /tmp/options_backup_*.sql

compatibility

  • Tested with CyberPanel/OpenLiteSpeed
  • WordPress 6.x compatible
  • WooCommerce-specific optimizations included

Performance benchmark

Test # loading time before/after optimization for i in {1..5}; do curl -o /dev/null -s -w "Test $i: %{time_total}s\n" https://domain.com done

Recommended limits

  • Optimal: < 100 autoloaded options, < 100 KB
  • Acceptable: < 200 options, < 800 KB
  • Problematic: > 300 options or > 1 MB

With 3 KB of autoloaded data, performance optimization is maximized – fewer options mean less PHP array processing with each WordPress request.

Content

    Leave a Reply

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

    lk media – your partner for online marketing & web design.

    Your success online is what drives us.
    lkmedia white
    Agency for future-oriented online marketing and web design. We are a team of creative minds and strategic thinkers who are passionately committed to your success in the digital space. Our goal is to present your brand optimally, reach your target group and generate sustainable growth.
    © Copyright 2024 lkmedia
    Sitemap Data protection imprint
    en_USEnglish