How to clear WordPress cache directory using a cron Job

At some point, owners of WordPress website will experience this cache directory “problem.” Yes, we love our WordPress optimization and caching plugins, but occasionally they, doing all sorts of nasty things, like causing backup scripts to simply fail without a whimper, or cause the hosting company to grumble about your space or files usage.

Messages such as:

“It appears your cache directory is using 99% of your website’s available.”

or

“Autoptimize’s cache size is getting big, consider purging the cache.”

In quick 3 steps, you can easily avoid them:

Set Up a Cron Job:

Assume your site home directory: /home/username/public_html/htdocs/
and wordpress cli directory: /home/username/wordpress/bin

Method #1: Use the command line (SSH)

  1. Log in to your account using SSH.
  2. At the command prompt, type the following command:

sudo crontab -e

Add the following lines, at the end:

# WordPress Keep it clean
10 1 * * * su daemon -s /bin/sh -c "find /home/username/public_html/htdocs/wp-content/cache/ -mtime +7 -delete"

WordPress cli for Autoptimize plugin

14 1 * * * su daemon -s /bin/sh -c "cd /home/username/public_html/htdocs/; /home/username/wordpress/bin/wp cache flush"
18 1 * * * su daemon -s /bin/sh -c "cd /home/username/public_html/htdocs/; /home/username/wordpress/bin/wp cache clear"

Method #2: Use cPanel:

  1. Log in to cPanel.
  2. In the Advanced section of the cPanel home screen, click Cron jobs.
  3. Under Cron Email, type the e-mail address that you want to receive notifications, and then click Update Email. Every time the cron job runs, the e-mail account will receive a message. If you do not want to receive e-mail notifications for the cron job, you can append >/dev/null 2>&1 to the command, which redirects all output to /dev/null.
  4. Under Add New Cron Job, in the Common Settings list box, select once in a day.
  5. In the Command text box, type the following line: cd ${HOME}/public_html; /usr/local/bin/php -q wp-cron.php
    This line assumes that you installed WordPress in the document root (public_html) directory. If you installed WordPress in another directory, modify the cd command to change to that directory instead.
  6. Click Add New Cron Job. The new cron job settings take effect immediately.

# WordPress Keep it clean
10 1 * * * su daemon -s /bin/sh -c "find ${HOME}/public_html/wp-content/cache/ -mtime +7 -delete"


Note: The schedule set it to run once a day

Using Bitnami stack with autoptimize plugin on AWS or Google?

  1. Log in to your account using SSH.
  2. At the command prompt, type the following command:

sudo crontab -e


# WordPress Keep it clean
10 1 * * * su daemon -s /bin/sh -c "find /opt/bitnami/apps/wordpress/htdocs/wp-content/cache/ -mtime +7 -delete"
14 1 * * * su daemon -s /bin/sh -c "cd /opt/bitnami/apps/wordpress/htdocs/; /opt/bitnami/apps/wordpress/bin/wp cache flush"
18 1 * * * su daemon -s /bin/sh -c "cd /opt/bitnami/apps/wordpress/htdocs/; /opt/bitnami/apps/wordpress/bin/wp cache clear"

# WordPress Plugin autoptimize - avoid cache size is getting big
24 1 * * * su daemon -s /bin/sh -c "cd /opt/bitnami/apps/wordpress/htdocs/; /opt/bitnami/apps/wordpress/bin/wp autoptimize clear"

Using words-rocket?

https://docs.wp-rocket.me/article/494-how-to-clear-cache-via-cron-job

Share:

More Posts

Share on facebook
Share on twitter
Share on linkedin

Sign up for our newsletters

SEO

Book Now Free SEO Consultation

Commerce

Take your ecommerce site to next the level

Development

Website Development