Disclaimer

This blog is kind of my own personal work Notebook, and the processes below are only guaranteed to work for my specific configurations. So, use this info at your own risk, and please understand I won't be able to provide any help if you break something

Tuesday, May 26, 2015

Automatically delete expired WooCommerce Transients in WordPress

Transients are used to store information into the WordPress database with an expiration time, the server load does not increase, and in most scenarios, it helps to speed the site.

However if your WordPress database options table (wp_options) accumulates old, expired transients that didn't get automatically cleared, they could end up bloating your database and even slowing down your website.

WooCommerce has a tool that allows you to the Products' Transient Cache and all the expired transients from WordPress. You can access it under WooCommerce > System Status > Tools



However if you need to delete the expired WooCommerce Transients (or any other Transients for that matter) in an automated manner, you have to implement some kind of Cron Job. Cron is a time-based job scheduler that allows you to run repetitive tasks unattended.

A "real" Cron Job always runs on the (Linux) server side, and generally speaking if you have access to the server tools needed to set it up, is always a much preferred solution. A WordPress Cron on the other hand is rather a pseudo-cron system, that is limited by several conditions. If you would like to dive into the technicalities here is an article that explains the differences in detail:
http://www.smashingmagazine.com/2013/10/16/schedule-events-using-wordpress-cron/

Since in most cases you will not have access to those server tools, you can place the following snippet into your functions.php file and then visit your site to force it to run:
 global $wpdb; $sql = 'DELETE FROM ' . $wpdb->options . ' WHERE option_name LIKE "_transient_%"'; $wpdb->query($sql);

I was about to modify such script to purge expired transients on a daily basis for a WooCommerce installation, but it turns out someone has already come out with a solution, the 'Delete Expired Transients' plugin. I checked its code and it seems to be well-written and optimized for performance, using few resources. You can get it here:
https://wordpress.org/plugins/delete-expired-transients/

After you install and activate the plugin, the first thing it does is to schedule a housekeeping task to delete expired transients. This means that there may not be any transients found when you visit its settings page under the tools menu right after installing the plugin, because they may have already been deleted.


That means that you'll probably never need to delete expired transients manually, because they'll be automatically deleted on a daily basis, which should be more than enough to keep the database uncluttered under any situation.

12 comments:

  1. I am very glad that I find your regular post here. Which seems to be very important and it made good time pass for me. I don’t actually comment and don’t like to spend time in typing the comment. But here I have to do this because this deserves a good like. Good wishes, from GrafWeb.

    ReplyDelete
  2. Does the idea of creating and managing a website for business scare you? If yes, then WordPress Development Services is the ideal solution for you. Hire WordPress developers and they will create unique business website and blogs which are easy to manage.

    ReplyDelete
  3. To fulfill your online store design & functionality requirements Hire WordPress WooCommerce Developer who are expert in programming and helps you in reach out your business goals. You can Hire WooCommerce Developer from our company from our company in affordable price. we are renowned for best quality solutions within goven deadline so hire dedicated WooCommerce Experts India make your online marketplace a unique one that attracts everyone and grows your online sales. our company has Expert WooCommerce Programmers in India that have potential to take challanges relevant to their skills.

    ReplyDelete
  4. Hi,

    Thanks for sharing a very interesting article about Automatically delete expired WooCommerce Transients in WordPress. This is very useful information for online blog review readers.Keep it up such a nice posting like this.

    Regards,
    WondersMind,
    Web Design Company Bangalore

    ReplyDelete
  5. The post is really good, we will use above mentioned strategy in our Web Development agency Bangalore.

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. Hei, jeg vil dele mitt fantastiske vitnesbyrd Etter å ha vært i forhold til Ektemann i årevis, brøt han med meg, jeg gjorde alt for å bringe ham tilbake, men alt var forgjeves, jeg ville ha ham tilbake så mye på grunn av kjærligheten jeg har For ham ba jeg ham om alt, jeg gjorde løfter, men han nektet. putlocker

    ReplyDelete