Recently a client of mine, who had a WooCommerce shop set up asked me to look into why his site started crashing all of a sudden. My first troubleshooting step was to disable all plugins and revert to the TwentyFifteen theme to see if there was a problem with a plugin or the theme we were using. Unfortunately that didn’t help the problem much. The next step was to look at the database to see if that was getting overloaded. It was, and it turns out there were way too many WooCommerce
transient_timeout rows in the
What are WooCommerce wc_session and transient_timeout?
wc_session are saved to your
wp_options table and contain your customer’s shopping cart information – without this the customer wouldn’t be able to add products to the cart, or checkout.
Basically when the user comes to your site and adds a product to the cart, a new session is created if it doesn’t already exist. This session gets stored in a cookie, and the data associated with this user (products in the cart) get stored using
wp_session in the database.
transient_wc_ is part of the transient system in WordPress, which provides a way of storing cached data in the database temporarily. According to the WordPress documentation the transients should “die naturally of old age” after their expiration passes.
Why do wc_session build up in the database?
Both the sessions and transients are supposed to get cleared by the wp_cron process, but on occasion those tasks aren’t able to complete correctly. Your site may have a lot of traffic, which causes a lot of sessions to be created. When your site tries to clear everything that shouldn’t be in the database anymore it may timeout if it hits server limits, causing the sessions to remain in the database. The next time it tries to clear the database it faces the same problem because the old stuff that should have already been removed is still there. The vicious cycle continues until you start to notice your site getting sluggish.
When search engines crawl your site they tend to follow links, including the “Add to Cart” links that are on each product and category page. Even though these bots aren’t live customers who you would want to be purchasing from you, they are creating cart sessions every time they follow the “Add to Cart” link. This could also be another factor that causes bloated databases.
How to clean up wc_session and transients?
Luckily, WooCommerce has a built in method for manually clearing the WooCommerce wc_session and transients from the database.
On your site, navigate to WooCommerce > System Status. From there click the Tools tab at the top. You should be seeing a screen like in the screenshot.
If you click on the buttons in the three rows highlighted in the screenshot (WC Transients, Expired Transients, and Customer Sessions) you should be able to clear your database of the excess that is slowing your site down. However, sometimes your database has grown so large that this process fails because your server hits the PHP max execution time, or exceeds its memory limits.
If you are finding that your site can’t handle the load by using these buttons, you can clear them manually by using a MySQL query in phpMyAdmin, or via shell if your host provides you with that access.
Back up your database before continuing! You were warned.
This query will remove any WooCommerce wc_sessions from your database.
This query will remove any transients set by WooCommerce from your database.
On the site I was cleaning up, this was the before and after wp_options table.
How to prevent WooCommerce wc_session buildup?
As I mentioned earlier, there are a few reasons why the wc_sessions build up, so how you prevent them will depend on your individual case.
There are a few things you can do if you are unsure why your database is growing.
First, look at your site access logs. Do you notice a lot of bot traffic? Or is your site getting a lot of actual human visitors?
If you are getting a lot of legitimate bot traffic from search engines like Google or Bing, you can simply put a couple lines in your site’s robots.txt to prevent them from following the Add to Cart links.
This will prevent “good” bots from following those links, but if you are getting a lot of spam bots on your site, this will likely do nothing. You may need to look into other solutions to prevent the spammers from accessing your site altogether. That’s content for another post though.
If you are getting a lot of actual human visitors, you probably need to look at upgrading the server your site is hosted on. Talk to your web host to see if they can recommend what plan would be able to best handle the traffic your site gets.
Another problem could be that your site is not able to run the wp-cron process. If that is the case, you can check your wp_config.php file to see if wp-cron is disabled. Open wp_config and look to see if it includes
define('DISABLE_WP_CRON', 'true');. If it does, remove that line from the file.
There may be a plugin that is preventing wp-cron from running as well. You might need to look at your site’s plugins or even theme to see if there is something conflicting with wp-cron.