如何在Mac/Windows终端重置WP Rocket缓存?实现部署后自动清缓存
Absolutely! You can absolutely reset WP Rocket's cache right from your Mac or Windows console, and setting up automatic cache clearing after deploying code from GitHub to production is totally doable—no more manual trips to the WordPress admin panel. Let's break this down step by step:
1. Manual Cache Clearing via Mac/Windows Console
WP Rocket officially supports WP-CLI commands, which is the key to managing cache from the console. Here's how to do it:
- First, make sure WP-CLI is installed on your server (or local dev environment if you're testing locally). It's a quick setup—you can install it via curl or download the phar file directly.
- Open your terminal (Mac) or Command Prompt/PowerShell (Windows).
- Navigate to your WordPress site's root directory. For example:
- On Mac:
cd /Applications/MAMP/htdocs/your-wordpress-site(adjust the path to match your local setup or server path if connecting via SSH) - On Windows:
cd C:\wamp64\www\your-wordpress-site(tweak this to your local environment)
- On Mac:
- Run the appropriate WP Rocket command based on what you need to clear:
- Clear all cache:
wp rocket clean - Clear only page cache:
wp rocket clean --cache - Clear static files cache (CSS/JS, media):
wp rocket clean --files - Clear minified files cache:
wp rocket clean --minified - Clear Critical CSS cache:
wp rocket clean --critical-css
- Clear all cache:
2. Automatic Cache Clearing After GitHub Deployment
To skip manual cache clears post-deployment, you can add the WP Rocket cache command to your CI/CD workflow. GitHub Actions is the most common tool for this—here's a simplified example:
Create a .github/workflows/deploy.yml file in your repo with these steps:
name: Deploy to Production on: push: branches: - main # Trigger on pushes to your production branch jobs: deploy: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v4 # Add your deployment steps here (e.g., rsync, FTP, or a tool like Deployer) - name: Deploy files via rsync run: rsync -avz --exclude='.git' ./ your-server-username@your-server-ip:/path/to/wordpress/site/ # Clear WP Rocket cache after deployment - name: Clear WP Rocket cache uses: appleboy/ssh-action@v1.0.3 with: host: ${{ secrets.SSH_HOST }} username: ${{ secrets.SSH_USERNAME }} key: ${{ secrets.SSH_PRIVATE_KEY }} # Use SSH keys for secure authentication script: cd /path/to/wordpress/site && wp rocket clean
Key Notes:
- Add your server's SSH credentials (host, username, private key) as secrets in your GitHub repo settings to keep them secure.
- Ensure WP-CLI is installed on your production server, and the SSH user has write permissions to the
wp-content/cachedirectory. - If you use a different deployment tool (like Deployer), add the cache clear command as a post-deploy hook. For example, in Deployer's
deploy.php:
after('deploy', function () { run('cd {{release_path}} && wp rocket clean'); });
That's it! With these steps, you can either manually clear cache from your local console or set it up to happen automatically every time you deploy code from GitHub—saving you the hassle of logging into the WP admin every time.
内容的提问来源于stack exchange,提问作者Tomasz Muter




