too large wp_woocommerce_sessions in Database [How-To Fix]

As I said in other articles, WooCommerce it is an increasingly robust and flexible module, able to transform WordPress a Ideal online store for a start-up business.

For an online store of size modeste WooCommerce it can also be run on "shared hosting", being able to easily support 5.000 products / 100 orders per day, if the theme used does not consume many resources and you do not abuse other modules (plugins).

Source code optimization WP Themes, modules (WP Plugins) and database optimization there are also two aspects that we need to be very careful about.

I noticed the other day that an online store of sizes modeit starts to consume enormously many RAM resources and CPU on a dedicated server generous. This high consumption of resources led to an increased "load" on the server and a long loading time of the onine store pages.

When we notice an unjustifiably high load on a web server, it is good to carefully investigate the causes before taking any action.

In the database I noticed that the table "wp_woocommerce_sessions"It's huge. It had over 6 GB. A huge volume for a table in a database that normally did not exceed 100 MB in total.

what is wp_woocommerce_sessions ?

As we can guess from his name, the table "wp_woocommerce_sessions”Contains PHP sessions (PHP Sessions).
These sessions are actions of website users (or web robots) and most of the time they reach the database through cookies. In wp_woocommerce_sessions are stored data about the products placed by users in the shopping cart, coupons, shipping fees, customer data and many other information related to the product ordering process.

These sessions are created whether or not the user is registered on the site and normally should automatically expire and delete after a while.

Unfortunately, it does not always happen that these sessions are deleted and, in some cases, they remain permanently stored in wp_woocommerce_sessions, which makes this table reach a fairly large volume.

How to delete the table wp_woocommerce_sessions from SQL?

1. go to Dashboard →  WooCommerce →  Status →  Tools (tab).

2. Scroll down to the option "Clear customer sessions“. Careful! Deleting customer sessions means deleting all the products they put in the shopping cart. If while deleting these sessions there are customers who have products in the cart, they will disappear and the possible online order will not be completed.

3. Click "Clear" and confirm the operation.

At this moment the table "wp_woocommerce_sessions" is empty, so all customer shopping sessions have been deleted.

The problem is far from solved. SQL table wp_woocommerce_sessions will collect data in it again and by default it will get proportions again, which is not desirable.

How do we stop client sessions from being permanently stored in WooCommerce - wp_woocommerce_sessions?

I have shown above how the table "wp_woocommerce_sessions" from the database, but the problem will appear again after a few days when client sessions will gather again.

Before WooCommerce 2.5, client sessions were captured through cookies, to be saved in the table later WordPress "wp_options".
For larger stores, this method has often led to disasters. “Wp_options”Being the vital table of WordPress, used by general options and settings. Identifying and manually deleting client sessions stored in wp_options it was not an easy job.
Performance WooCommerce they were not the best, and scalability over time was a sensitive point.

With the launch WooCommerce 2.5 in 2015, the developers WooCommerce introduced a new session handling and storage system WooCommerceBased on WP Sessions Manager. This system led to the emergence wp_woocommerce_sessions. A dedicated table for client sessions, which in the database does not interact with other tables. In case of major errors, the losses should be minimal.

From what I have observed, the online stores launched before WooCommerce 2.5 and which have received constant updates over time, have some problems regarding the automatic deletion of client sessions. It is most likely a bug that appeared on a newer version of WooCommerce 2.5 or a plugin WordPress / WooCommerce which has not kept up with the updates.

In my case it was a conflict with a function added to the file functions.php of the theme, and which prevents automatic deletion after a time of expired sessions from woocommerce_sessions.
If you are facing such a problem you need to carefully investigate all possible causes. SQL error, SQL permissions, Cron Job, conflicts with other plugins and last but not least check the changes made by you in the code over time.

I saw that WooCommerce offers for only $29 a plugin capable of managing customer sessions. "Clear Cart and Sessions for WooCommerce“. Of course, in addition to the scheduled deletion of sessions, the module has several tools that could help you.

A simpler option scheduled deletion of “woocommerce_sessions”At one-day intervals, is available with the following code in functions.php:

// clean cart every day
if (!wp_next_scheduled('cron_wc_clean_cart')) {
    wp_schedule_event( time(), 'daily', 'cron_wc_clean_cart' );
}

add_action ( 'cron_wc_clean_cart', 'wc_clean_session_cart' );
function wc_clean_session_cart() {
    global  $wpdb;

    $wpdb->query( "TRUNCATE {$wpdb->prefix}woocommerce_sessions" );
    $wpdb->query( "DELETE FROM {$wpdb->usermeta} WHERE meta_key='_woocommerce_persistent_cart_" . get_current_blog_id() . "';" );
    wp_cache_flush();
}

Leave a comment if you need help or have another solution.

Passionate about technology, I enjoy writing on StealthSettings.com since 2006. I have a rich experience in operating systems: macOS, Windows, and Linux, as well as in programming languages and blogging platforms (WordPress) and for online stores (WooCommerce, Magento, PrestaShop).

How to » Tweaks & Hacks » too large wp_woocommerce_sessions in Database [How-To Fix]

1 thought on “Too large wp_woocommerce_sessions in Database [How-To Fix]"

  1. I have no way to delete the data weighs 500mb and I have a limit of 1000mb per minute to delete everything is completely filled again… .some solution?

    Reply
Leave a Comment