Caching, Development, Magento, Web

Automatically Clear Magento Cache and Session

magento magento

While working on a magento based project for one of my friends, I was facing an issue where the magento site started to respond really slow and at times the site just won’t load and show a blank white page. While researching I figured this happened because magento manages cache and sessions at various different levels and to keep track of the caches and sessions it creates and manages these files under /var/cache/ and /var/session/ directories.

I deleted all the files and directories from the above mentioned 2 directories and voila! The site was up and running again without any problems and page load performance was much better than before. I went ahead and informed my friend that the “blank white page” problem has now been resolved. Within 24 hours of our communication, I got to know that he is facing the same problem once again and this time I knew that this is an on-going magento process and I have to come-up with a better permanent solution.

Permanently resolve magento blank white page error

So I decided to write a blog post about How to resolve the infamous Magento Blank White Page Error  while I fix it myself for my friend’s magento site. Following is a brief of how we would resolve this issue and make sure that our magento website performs better.

  •  Write a bash script that clears cache and session data
  • Create a cronjob to run the bash script every 12 hours

Write a bash script that clears the cache and session data

Bash Script to Clear Magento Cache and Session Data

Bash Script to Clear Magento Cache and Session Data

As we know that we need to clear the magento cache and session data whenever it builds up and starts to slow down the website. Also, we know that this is a repetitive process and magento builds up good enough cache and session data within 24 hours. So we will need to clear it every time before it chokes your magento website to a blank white page. To resolve this we will write a simple bash script that takes care of clearing the cache and session data.

Writing a bash script will help us run just one command to clear both the “cache” and “session” directories, instead of doing them manually each time. So let’s get started with it. Login to your server as root and create a file named magento_cache_syrup.sh and put in the following code.

Well, to make this even easier for you I have created a github repo called magento-cache-syrup and pushed the above code to it. So instead of creating the above file you can simply clone the above github repository.

Make sure that you change the path in the above code and point it to your magento installation. For absolute beginners, simply replace “/var/www/sl60” with the path to your magento website directory.

Once you have corrected the path you can simply run the above script by calling it from the terminal.

This should clear all the data in cache, session, tmp and reports directories in your magento installation. But the problem does not end here. We need to make sure that we do it every time before we reach the blank white page. To solve this, we will enable a cron job on the server that runs our bash script (magento-cache-syrup) every 12 hours.

Create a cronjob to run the bash script every 12 hours

Create Cronjob to Clear Magento Cache and Session Data

Create Cronjob to Clear Magento Cache and Session Data

In order to create a cronjob that runs every 12 hrs, Login to your server and run the following command.

Firstly, it will prompt you choose an editor that you would like to use to edit the crontab configuration file. I love vim but you please feel free to use editor of your choice :). In order to enable the cronjob, Go to the end of the file and add the following line to it.

Do not forget to replace /path/to/your/ with the actual path to the bash script you created or cloned off the magento-cache-syrup repository. Now simply save and exit the file and now you’r server will run this script every 12 hours which will automatically clear all the cache and session data.

If you have any questions, queries or suggestions, please do so via the disqus comments below. You can also reach me on twitter at @mytharora.

You Might Also Like

  • Chris Petitclerc

    I logged into cpanel / created a file at root magento_cache_syrup.sh / then added the code with the proper path to root (for me was /home/sharpinc/public_html/var/report). Then went to create a cron job in cpanel every 12 hours with command (crontab -e/home/sharpinc/public_html/magento_cache_syrup.sh)

    • geekunlimited

      If you are installing a cronjob via cPanel, then in the command text box you just need to give the path to the script you want to run (i.e. excluding crontab -e). So change your cronjob command to just “home/sharpinc/public_html/magento_cache_syrup.sh” and it should work.

  • Chris Petitclerc

    I get this message in my email. What does it mean?

    crontab: invalid option — /
    crontab: usage error: unrecognized option
    usage: crontab [-u user] file
    crontab [-u user] [ -e | -l | -r ]
    (default operation is replace, per 1003.2)
    -e (edit user’s crontab)
    -l (list user’s crontab)
    -r (delete user’s crontab)
    -i (prompt before deleting user’s crontab)
    -s (selinux context)

  • Chris Petitclerc

    I got this in my email from Cpanel:
    crontab: usage error: no arguments permitted after this option
    usage: crontab [-u user] file
    crontab [-u user] [ -e | -l | -r ]
    (default operation is replace, per 1003.2)
    -e (edit user’s crontab)
    -l (list user’s crontab)
    -r (delete user’s crontab)
    -i (prompt before deleting user’s crontab)
    -s (selinux context)

    Is there any problems?