How to Clean and Optimize the WooCommerce Sessions Table

Published on March 3, 2025 by Sebastian Moran in MainWP Blog under About, Tips & Tricks
Heads up: This page may include affiliate links. Read the full disclaimer.
A computer screen displaying WooCommerce optimization tools and session management settings, illustrating the process of cleaning and optimizing the WooCommerce sessions table for improved database performance.

Maintaining an optimized database is crucial for the performance of any WordPress site, especially when running an e-commerce store with WooCommerce. One of the tables that can contribute to database bloat over time is the woocommerce_sessions table. This table stores user session data, such as cart contents and other temporary information, but it can accumulate expired session data if not regularly cleaned. In this article, we’ll optimize the woocommerce_sessions table to ensure your store runs smoothly.

1. Backup Your Database

Before making any changes, it’s essential to back up your database. Optimizing and cleaning up your database involves making alterations that could impact data, so always ensure you have a full backup to revert to in case of any issues. You can use plugins like UpdraftPlus or Duplicator for regular backups.

2. Clear Expired Sessions

The woocommerce_sessions table stores data such as active user sessions, cart contents, and other temporary information. Over time, these sessions can accumulate, especially if users abandon their carts or visit your store but do not complete any transactions.

To maintain a healthy database, expired sessions should be cleaned regularly. WooCommerce automatically removes expired sessions, but you can manually delete them using an SQL query.

To remove expired sessions from the woocommerce_sessions table, use the following query:

DELETE FROM wp_woocommerce_sessions WHERE session_expiry < UNIX_TIMESTAMP();

This SQL command deletes any session records where the session expiry time is past, effectively removing old, unused session data. You can run this query via your hosting provider’s database management tool, phpMyAdmin or Adminer.

Additionally, you can set up a plugin or cron job to delete expired sessions regularly, ensuring the table remains clean without needing manual intervention.

WooCommerce has a built-in tool to be able to clear customer sessions if needed it can be found in wp-admin from;

WooCommerce > Status > Tools > Clear customer sessions

When you click on the clear button it will provide a warning that this will be clearing all active sessions and saved carts.

3. Optimize the Table

Once you’ve cleared the expired sessions, the next step is to optimize the woocommerce_sessions table itself. Over time, as data is added and removed from the table, it can become fragmented, leading to slower queries and a heavier database. To address this, you can optimize the table by running the following SQL command:

OPTIMIZE TABLE wp_woocommerce_sessions;

This command reorganizes the table, reclaims unused space, and can improve query performance. Running this command periodically is recommended, particularly if your store experiences a lot of user traffic or has a large number of sessions.

4. Limit Session Data Storage

By default, WooCommerce stores session data for all visitors, including those who may not make any purchases. If your store doesn’t require long-term session storage, you can modify the session expiration time to reduce the load on the database.

You can adjust the session expiration time by adding a custom function to your theme’s functions.php file or using a code snippet. For example, to limit session data to just 24 hours, use the following code smippet:

add_filter( ‘woocommerce_session_expiration’, ‘custom_session_expiration’ );

function custom_session_expiration( $expiration ) {

    return 86400; // 24 hours in seconds

}

This code limits session expiration to 1 day (86400 seconds). Adjust the value as necessary for your store’s needs.

5. Use a Plugin for Database Optimization

If you prefer a more automated approach, several plugins can help you optimize and clean your WooCommerce database. WP-Optimize and Advanced Database Cleaner are excellent choices for managing database optimization tasks without requiring manual SQL queries. These plugins also offer additional features, such as scheduled cleanups, which can ensure that your woocommerce_sessions table remains optimized over time.

Optimizing the woocommerce_sessions table is essential to maintaining a fast and efficient WooCommerce store. Regularly cleaning expired sessions, optimizing the table, and limiting session storage can significantly improve your site’s performance. Following these simple steps ensures that your WordPress site runs smoothly and provides a better customer experience.

Useful Links

https://wordpress.org/support/topic/clearing-all-customer-sessions-data-loss

Share

Manage Unlimited WordPress Sites from One Dashboard!

  • Privacy-first, Open Source, Self-hosted
  • Easy Client Management
  • 15+ & 30 + Premium Add-ons
  • Bulk Plugins & Themes Management
Get Pro Now

Categories

Recent Posts

Search MainWP.com

[searchwp_form id="1"]