WP Manifestindependent plugin directory
manifest / ecommerce / woocommerce-stale-order-cleanup

WooCommerce Stale Order Cleanup

Background cleanup of stale WooCommerce orders with Action Scheduler, configurable retention rules, and HPOS support.

★ 0stars

Install

Download source zip No release zip yet

This is the repository archive. It installs, but the folder name will carry the branch suffix and updates will not flow.

Install straight from GitHub with WP-CLI:

wp plugin install https://github.com/amirrezashf/woocommerce-stale-order-cleanup/archive/refs/heads/main.zip
No built-in updater. New versions won't appear under Dashboard → Updates. Install new releases yourself, or let Git Updater track the repository.

Lightweight WooCommerce plugin that cleans up stale orders in selected statuses using WooCommerce CRUD APIs and Action Scheduler.

Features

  • Targets pending, failed, and cancelled orders by default.
  • Default retention period: 10 minutes.
  • Runs asynchronously every hour via WooCommerce Action Scheduler; it never blocks a frontend request with sleep().
  • Uses wc_get_orders(), wc_get_order(), and WC_Order::delete() for HPOS/legacy-storage compatibility.
  • Moves matching orders to Trash by default rather than permanently deleting them.
  • Processes orders in bounded batches.
  • No custom tables, frontend assets, personal-data logging, or external dependencies beyond WooCommerce.

Requirements

  • WordPress 6.0+
  • PHP 7.4+
  • WooCommerce 7.0+

Installation

  1. Upload the plugin ZIP through Plugins > Add New > Upload Plugin.
  2. Activate the plugin.
  3. WooCommerce Action Scheduler will schedule the cleanup job.

Configuration

There is intentionally no settings screen. Developers can adjust behavior using filters:

add_filter( 'wcsoc_retention_seconds', function () {
    return 30 * MINUTE_IN_SECONDS;
} );

add_filter( 'wcsoc_target_statuses', function () {
    return array( 'pending', 'failed' );
} );

add_filter( 'wcsoc_batch_size', function () {
    return 100;
} );

Permanent deletion is disabled by default. If explicitly required:

add_filter( 'wcsoc_force_delete', '__return_true' );

Use permanent deletion cautiously because it is destructive and may conflict with accounting, audit, legal, payment-provider, or operational retention requirements.

How it works

The original snippet attempted to inspect an order on init, call sleep(600), and then invoke wp_delete_post(). That design cannot correctly receive an order ID from init, blocks a PHP worker for ten minutes, and bypasses WooCommerce order CRUD/HPOS abstractions.

This plugin instead schedules a recurring background action. Each run queries only stale order IDs, re-checks the current order status, and deletes through WooCommerce CRUD.

HPOS compatibility

The plugin declares compatibility with WooCommerce High-Performance Order Storage and does not access wp_posts or wp_postmeta directly for order operations.

Security and data integrity

There are no public endpoints or browser-controlled deletion requests. Cleanup criteria are determined server-side. Matching orders are trashed by default so accidental cleanup is recoverable.

Privacy

The plugin stores no additional customer data and creates no activity logs.

Performance

Cleanup runs outside normal page rendering. Queries are bounded and return order IDs rather than hydrating an unbounded set of order objects. Batch size defaults to 50 and is capped at 200 per query.

Limitations

  • A 10-minute retention threshold does not mean deletion occurs exactly at minute 10; the recurring worker runs hourly by default.
  • WordPress/WooCommerce background processing depends on WP-Cron/Action Scheduler execution on the host.
  • Automatically removing failed or cancelled orders may be undesirable for stores that require long-term audit/accounting history. Adjust the target statuses or retention period accordingly.

Suggested GitHub Description

Safely clean up stale WooCommerce orders in background batches using Action Scheduler, WooCommerce CRUD, and HPOS-compatible APIs.

License

GPL-3.0

Author

Amirreza Shayesteh Far
https://github.com/amirrezashf


فارسی

افزونه‌ای سبک برای پاک‌سازی سفارش‌های قدیمی WooCommerce در وضعیت‌های مشخص، بدون اجرای sleep() در درخواست‌های سایت و با استفاده از Action Scheduler و WooCommerce CRUD.

قابلیت‌ها

  • وضعیت‌های پیش‌فرض: pending، failed و cancelled.
  • زمان نگهداری پیش‌فرض: ۱۰ دقیقه.
  • اجرای background هر یک ساعت با Action Scheduler.
  • سازگار با HPOS و storage قدیمی WooCommerce.
  • انتقال سفارش به Trash به‌صورت پیش‌فرض، نه حذف دائمی.
  • پردازش batch برای جلوگیری از query و memory نامحدود.
  • بدون ذخیره اطلاعات شخصی یا ایجاد جدول اضافی.

نکته مهم

نسخه اولیه با init، sleep(600) و wp_delete_post() طراحی شده بود. این روش هم order ID معتبری از init دریافت نمی‌کند، هم PHP worker را ۱۰ دقیقه اشغال می‌کند و هم HPOS را دور می‌زند. نسخه افزونه این مشکلات را با background scheduling و WooCommerce CRUD برطرف می‌کند.

حذف دائمی عمداً پیش‌فرض نیست، چون سفارش‌های failed و cancelled ممکن است برای گزارش مالی، audit یا عملیات فروشگاه لازم باشند.

مجوز

GPL-3.0

نویسنده

Amirreza Shayesteh Far
https://github.com/amirrezashf