☰
WORDPRESS / PRACTICE

How to Clean Up Expired WooCommerce Sessions Without Risking Carts

How to Clean Up Expired WooCommerce Sessions Without Risking Carts

WooCommerce stores guest carts and sessions in a table such as `wp_woocommerce_sessions`. On a busy store, this table can accumulate records from visitors who left the site long ago. If cleanup does not run or is delayed, the table grows, increasing backup sizes and database load.

You should never clear the entire table because it may contain customers’ active carts. The safe approach is to first estimate the number of expired records, verify that WooCommerce’s scheduler is working, and, if necessary, delete only old sessions in small batches.

What the WooCommerce Sessions Table Stores

The `wp_woocommerce_sessions` table contains session data, including carts belonging to unauthenticated visitors. The `wp_` prefix is standard, but the actual prefix may differ on your site—for example, `shop_woocommerce_sessions`. Check the exact table name before running any SQL.

For each record, WooCommerce stores the session expiration time in the `session_expiry` field. This is a Unix timestamp—the number of seconds since January 1, 1970. A record is considered expired when its `session_expiry` value is lower than the current time.

  • Active sessions may contain products in a cart, so deleting them will cause a guest’s cart to be lost.
  • The total number of rows does not always indicate a problem: you must count expired records separately.
  • Do not rely only on the table size shown in your hosting panel—check its contents and cleanup status first.

How to Check the Number of Expired Sessions

Run the query in phpMyAdmin, Adminer, or another MySQL management tool. Replace `wp_woocommerce_sessions` with the actual table name. The `CASE` version returns zero instead of `NULL` when the table is empty.

  • Run the query against the database belonging to this WordPress site.
  • If the store is running on a production server, start with SELECT queries only.
  • Compare the result with the actual time of the scheduler’s most recent run.
SELECT
    COUNT(*) AS total,
    SUM(CASE WHEN session_expiry < UNIX_TIMESTAMP() THEN 1 ELSE 0 END) AS expired
FROM wp_woocommerce_sessions;

Checking the Date Range

If the result looks unusual, check the oldest and newest `session_expiry` values. This can help identify an incorrect server time or records that are no longer being updated.

SELECT
    MIN(session_expiry) AS oldest_expiry,
    MAX(session_expiry) AS newest_expiry,
    FROM_UNIXTIME(MIN(session_expiry)) AS oldest_date,
    FROM_UNIXTIME(MAX(session_expiry)) AS newest_date
FROM wp_woocommerce_sessions;

Check WooCommerce’s Built-In Cleanup First

WooCommerce has its own session cleanup task, which runs through Action Scheduler. In the WordPress dashboard, open the scheduled actions area under WooCommerce and search for the `woocommerce_cleanup_sessions` hook. The names or locations of individual interface elements may vary slightly depending on the WooCommerce version and installed plugins.

Review the status of recent and upcoming runs. It is important to check not only whether the hook exists, but also whether it actually runs. If tasks have `Pending` or `Failed` statuses, or continue accumulating without running, the problem may be with the scheduler rather than the SQL.

  • Check the date of the most recent successful `woocommerce_cleanup_sessions` run.
  • Review the error log for failed actions.
  • Make sure the site runs WP-Cron or another mechanism for launching scheduled tasks regularly.
  • Look for a large queue of other pending tasks, which may delay session cleanup.

Why Action Scheduler May Not Be Working

Common causes include disabled or irregular WP-Cron, PHP errors, database access problems, blocked requests, an overloaded server, or a plugin conflict. If the scheduler is not running any tasks, do not limit the investigation to the sessions table: check the overall cron setup and the WordPress and server error logs.

Safely Delete Expired Records in SQL Batches

If the built-in cleanup cannot process a large table quickly enough, create a database backup and then delete records in batches. A batch of 1,000 rows reduces the duration of an individual query and the risk of extended locking compared with one large DELETE operation.

Repeat the query until it returns zero deleted rows. Run it manually with pauses or use a controlled server-side script. On a busy store, choose a period with the lowest activity.

  • Create and verify a backup before deleting anything.
  • Make sure the condition uses the correct `session_expiry` field.
  • Do not delete records based on an approximate date when you can compare them with the current Unix timestamp.
  • After each series of batches, check database load and log errors.
DELETE FROM wp_woocommerce_sessions
WHERE session_expiry < UNIX_TIMESTAMP()
LIMIT 1000;

How to Monitor the DELETE Result

The database tool usually shows the number of deleted rows after the query runs. Also repeat the SELECT query to count the remaining expired sessions. If the number does not decrease, check the table name, timestamp values, and any errors reported by the query.

  • Do not run multiple DELETE operations in parallel against the same table.
  • Avoid interrupting a query unnecessarily during peak traffic.
  • If the table is very large, test one batch on a copy or staging environment first.
SELECT COUNT(*) AS expired
FROM wp_woocommerce_sessions
WHERE session_expiry < UNIX_TIMESTAMP();

What Not to Do During Cleanup

The most dangerous mistake is to run `TRUNCATE TABLE` or delete the entire table. This removes both expired and active sessions. Visitors may lose their carts, which can lead to complaints and, in some cases, lower conversion rates.

You should also avoid manually deleting the table or changing its structure without understanding how the current WooCommerce session handler works. Cleaning up data and fixing an Action Scheduler problem are separate tasks: if the underlying cause is not resolved, the table will quickly fill up again.

  • Do not use `TRUNCATE` as a quick way to free space.
  • Do not delete sessions based on being “older than a few days” if you are not taking the `session_expiry` value into account.
  • Do not run mass cleanup without a backup.
  • Do not clear the object cache blindly: this may trigger additional requests and temporarily increase site load.

What to Check After Cleanup

After deleting expired records, confirm that the row count has decreased and that new sessions are still being created. Test the store in a private browser window: add a product to the cart, move between pages, and make sure the cart does not disappear.

Also test the checkout process in a safe environment if the store setup allows it. On a production site, do not create a real order without coordinating with your team. Then review Action Scheduler and confirm that the next cleanup run is scheduled and completes without errors.

  • Count the expired sessions again.
  • Test the guest cart.
  • Make sure scheduled actions are not stuck in the queue.
  • Over the next several hours or days, check whether the table is growing unusually quickly.
  • If the database uses a separate object cache through the hosting platform, check its status using the host’s tools instead of clearing the entire cache unnecessarily.

How to Prevent Sessions from Accumulating Again

Persistent table growth usually means that the scheduler is not running regularly or cannot process the queue quickly enough. After one-time cleanup, it is important to fix the underlying cause. Check WP-Cron, the system cron, PHP errors, execution time limits, and the condition of the database.

You should also review plugins that affect the cart, caching, or checkout process. Page caching must not incorrectly cache cart, checkout, or personal-data pages. If the problem continues after disabling suspicious extensions, involve your hosting provider or developer to analyze the logs.

  • Monitor the total number of sessions and the number of expired sessions regularly.
  • Watch for pending and failed Action Scheduler tasks.
  • Do not replace a functioning automatic process with permanent manual DELETE operations.
  • Schedule database maintenance around peak sales periods.
  • Before making changes to a production store, test the procedure on staging or with a current backup.

Frequently asked questions

Can I simply delete the wp_woocommerce_sessions table?

No. The table may contain active guest carts. Delete only records whose `session_expiry` has passed, preferably in batches and after creating a backup.

What should I do if expired sessions are not deleted automatically?

In Action Scheduler, check the `woocommerce_cleanup_sessions` hook, its recent runs, errors, and pending queue. Also verify that WP-Cron or another cron mechanism is running scheduled tasks.

Is it safe to run DELETE while the store is operating?

Batch deletion of expired records is generally less risky than one large query, but it still creates database load. Create a backup, use small batches, and run the process during a low-traffic period.

Why does the table grow quickly again after cleanup?

The most common reason is that Action Scheduler is not running or cannot keep up. Other causes may include high guest traffic, cron errors, plugin conflicts, or incorrect caching configuration. Check the logs and the regularity of scheduled tasks.

Conclusion

WooCommerce session cleanup should be selective: first determine how many records are expired, then check the built-in `woocommerce_cleanup_sessions` task, and only use batch SQL deletion when necessary.

A backup, load monitoring, and post-cleanup cart testing can help prevent active sessions from being lost. If the table grows quickly again, investigate Action Scheduler and cron instead of repeatedly performing manual cleanup.

← All WordPress materials