Automatically Clear Magento Cache and Session

Did you ever receive a blank white page on your website, it may be because of cache from Magento at the back. If the site does not load and is throwing this error we will show you how you can clear Magento cache and session with a permanent solution.

Magento manages cache and sessions at various separate levels and manages cache and session files under /var/cache/ and /var/session/  located inside the Magento installation directory. For example, if your Magento is installed in your main example_html folder, the cache will be in example_html/var/cache.

To clear the cache, simply delete everything from the /var/cache directory and then reload your website in your browser.

To clear all sessions, you can also delete everything from the /var/session folder within your Magento installation directory.

But the above option will resolve this issue temporarily, and then you will have to keep on clearing these cache files after specific intervals.

How to permanently solve the Blank white paper error?

To solve this we have to automate this process to clear Magento cache and sessions. We can run a bash script that runs through a specific interval and automatically clears cache from the directory.

Write a bash script to clear Magento cache and sessions

Magento easily collects cache and sessions in a short period of time say 24 hours. So we will have to write a bash script that will clear the cache and session data.

First we will create a file named Magento_cache_machine.sh and write the below code in it.

#!/bin/sh

rm -rf /var/www/path/var/cache/*
rm -rf /var/www/path/var/session/*
rm -rf /var/www/path/var/report/*

When copying the above code replace “/var/www/path/” with the path to your Magento website directory.

Now that the script is ready lets call it from the terminal.

$> cd  magento-cache-machine/
$>  magento-cache-machine.sh/ magento-cache-machine.sh

The above files will clear Magento cache, session, tmp, and report data. We want to automate this process. So we will now create a cronjob for this. To run cronjob on your website log in to your server and run the following command.

crontab -e

This will prompt you to choose an editor. Choose the one you are comfortable with. Now to enable cronjob to go to the end of the file and type the following command in it.

0 */15 * * * /pathtoyourfile/magento_cache_machine.sh

Replace the “/pathtoyourfile/” with the location of your bash script we created earlier. Now simply save and exit the file. We have set the interval to 15 hours. So every 15 hours the code will run and automatically clear Magento cache. If you want to change the timing simply change the number in the code.

Do let us know if this blog has helped you and share it with your colleagues and friends. If you have a better option to solve this issue you can comment below and let everyone benefit from it.

We will be happy to hear your thoughts

      Leave a reply

      Techs Tricks
      Logo
      Reset Password
      Shopping cart