WordPress Database Cleanup & Cloudflare Optimization Tutorial
Share this:

Cloudflare Bot Protection Setup

Step 1: Create Verified Bot Rule

  1. Go to Cloudflare Dashboard → SecurityWAF
  2. Click Custom rulesCreate rule
  3. Rule name: Allow Verified Search Engines
  4. Click Edit expression
  5. Paste this expression:

 (cf.client.bot) or (cf.verified_bot_category in {"AI Search" "AI Assistant" "Search Engine Crawler" "Advertising & Marketing"}) or (http.user_agent contains "OAI-SearchBot") or (http.user_agent contains "PerplexityBot") or (http.user_agent contains "ChatGPT-User") or (ip.src eq 118.179.207.90) or (ip.src.asnum in {63034 15169 36040})

  1. Action: Select Skip
  2. Check all boxes:
    • ✅ All remaining custom rules
    • ✅ All rate limiting rules
    • ✅ All managed rules
    • ✅ All Super Bot Fight Mode Rules
  3. Place at: First
  4. Click Save

Part 2: Cloudflare Cache Settings

Step 2: Set Browser Cache TTL

  1. Go to CachingConfiguration
  2. Find Browser Cache TTL
  3. Set to 1 day (24 hours)
  4. Click Save

Step 3: Create Static Assets Cache Rule

  1. Go to CachingCache Rules
  2. Click Create Rule
  3. Rule name: Long Cache for Static Assets
  4. Click Edit expression
  5. Paste this expression:

(http.request.uri.path.extension in {"jpg" "jpeg" "png" "gif" "webp" "svg" "css" "js" "ico" "woff" "woff2" "ttf" "eot" "mp4"})

  1. Set Browser Cache TTL: 1 month
  2. Set Edge Cache TTL: 1 month
  3. Click Save

Step 4: Purge Cache

  1. Go to CachingConfiguration
  2. Scroll down and click Purge Everything
  3. Confirm

Part 3: Database Cleanup (phpMyAdmin)

Step 5: Clean Post Views Table

  1. Open phpMyAdmin
  2. Select your WordPress database
  3. Click SQL tab
  4. Run this query:

DELETE FROM wp_post_views WHERE period < DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 30 DAY), '%Y%m%d');

  1. Run this query:

OPTIMIZE TABLE wp_post_views;

Step 6: Clean Rank Math Analytics

  1. Run this query:

DELETE FROM wp_rank_math_analytics_gsc WHERE created < DATE_SUB(NOW(), INTERVAL 60 DAY);

  1. Run this query:

OPTIMIZE TABLE wp_rank_math_analytics_gsc;

Step 7: Clean Rank Math Inspections

  1. Run this query:

DELETE FROM wp_rank_math_analytics_inspections WHERE created < DATE_SUB(NOW(), INTERVAL 60 DAY);

  1. Run this query:

OPTIMIZE TABLE wp_rank_math_analytics_inspections;

Step 8: Check Your Results

  1. Click Structure tab in phpMyAdmin
  2. Scroll to bottom to see new total database size
  3. Expected: 320-350 MB (from ~500 MB)

Part 4: WordPress Configuration

Step 9: Update wp-config.php

  1. Go to your hosting control panel → File Manager
  2. Navigate to /public_html/
  3. Right-click wp-config.phpEdit
  4. Add these lines before /* That’s all, stop editing! */:

// Disable post revisions
define('WP_POST_REVISIONS', false);  

// Auto-delete trash after 7 days

define('EMPTY_TRASH_DAYS', 7);

  1. Click Save

Step 10: Set Rank Math Data Retention

  1. WordPress Dashboard → Rank MathGeneral Settings
  2. Click Analytics tab
  3. Find “Data Retention Period”
  4. Set to 60 days
  5. Click Save Changes

Part 5: Schedule Regular Maintenance

Step 11: Set Calendar Reminders

Set reminders to run these queries every 3 months:

Clean old post views (keep last 30 days)
DELETE FROM wp_post_views
WHERE period < DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 30 DAY), '%Y%m%d');
OPTIMIZE TABLE wp_post_views;  

Clean old Rank Math data (keep last 60 days)

DELETE FROM wp_rank_math_analytics_gsc
WHERE created < DATE_SUB(NOW(), INTERVAL 60 DAY);
OPTIMIZE TABLE wp_rank_math_analytics_gsc; 

DELETE FROM wp_rank_math_analytics_inspections
WHERE created < DATE_SUB(NOW(), INTERVAL 60 DAY);
OPTIMIZE TABLE wp_rank_math_analytics_inspections;

Expected Results

Database Size:

  • Before: 500+ MB
  • After: 320-350 MB
  • Saved: ~150-180 MB

Performance Improvements:

✅ Faster page loads
✅ Reduced MySQL connections
✅ Better server performance
✅ Improved AdSense bot crawling
✅ Better PageSpeed scores

Quick Reference – Cleanup Schedule

TaskFrequency
Database cleanup queriesEvery 3 months
Check Cloudflare cache settingsEvery 6 months
Review Rank Math data retentionWhen analytics grow large
Purge Cloudflare cacheAfter major site changes

Troubleshooting

If Google Custom Search shows “No Results”:

  • Wait 30 minutes after setting up Cloudflare bot rules
  • Check Security → Events to confirm bots are being “Skipped”
  • Verify CSE configuration at https://programmablesearchengine.google.com/

If database doesn’t shrink after OPTIMIZE:

  • This is normal for InnoDB tables
  • Space is freed internally but may not show immediately
  • Performance improvements happen regardless

If AdSense earnings don’t improve:

  • Give it 2-3 weeks for Google to recrawl
  • Check ad placements (6-7 ads for 2000-word articles)
  • Monitor PageSpeed scores
  • Verify bots are accessing your site in Cloudflare logs

Done! ✅

Your WordPress site is now optimized for:

  • Better performance
  • Lower database bloat
  • Improved AdSense revenue potential
  • Efficient bot crawling

 

Recommended For You

Share this:

Leave a Reply

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