How to Programmatically Delete WooCommerce Orders

geekerhub

Updated on:

delete woocommerce orders

WooCommerce provide a way to delete order from admin screen, but when we consider large number of orders to be deleted due to some circumstances, then WooCommerce feature take long to delete all selected orders.

Discover the effective techniques to programmatically delete WooCommerce orders and optimize your order management workflow. By automating the deletion process, you can save valuable time and streamline your operations.

Whether you need to remove test orders, outdated transactions, or any unwanted orders, this guide will provide you with step-by-step instructions on how to achieve it programmatically. Say goodbye to manual deletion and embrace the power of automation to simplify your WooCommerce order management.

Clear your order list effortlessly, maintain a clutter-free database, and enhance the overall performance of your WooCommerce store. With programmatically deleting orders, you can easily customize the deletion criteria, such as order status, date range, or specific product categories, to suit your specific needs.

Start optimizing your order management process today and experience the benefits of automated order deletion with WooCommerce.To delete all orders except current year, we can use core MySQL query.To use core MySQL query, one must have knowledge on how WordPress store WooCommerce data in WordPress database.

Here is the below code snippet that you can execute to delete all orders except current year.You can execute this script using submit button or on load or on debug mode, whatever way is comfortable while development.

<?php
function geeker_delete_woocommerce_old_shop_order_callback(){
  global $wpdb;
  
  /*  
    Step 1
    First we need to delete all the postmeta associated with the shop_order post type and those post meta is not associated with the current year order
  */
  $delete_query = "DELETE
            FROM {$wpdb->prefix}postmeta
            WHERE post_id IN (SELECT ID FROM {$wpdb->prefix}posts WHERE post_type = 'shop_order' AND YEAR(post_date) != YEAR(NOW()));";
            echo $delete_query;
  $wpdb->query($delete_query);


  /*  
    Step 2
    Now delete all commentmeta associated with the shop_order post type
  */
  $delete_query = "DELETE
            FROM {$wpdb->prefix}commentmeta
            WHERE comment_id IN (SELECT comment_ID
                                 FROM {$wpdb->prefix}comments
                                 WHERE comment_post_id IN
                                       (SELECT ID FROM {$wpdb->prefix}posts WHERE post_type = 'shop_order' AND YEAR(post_date) != YEAR(NOW()))
            );";
  $wpdb->query($delete_query);


  /*  
    Step 3
    Now delete all comments associated with the shop_order post type
  */
  $delete_query = "DELETE
            FROM {$wpdb->prefix}comments
            WHERE comment_post_id IN
                  (SELECT ID FROM {$wpdb->prefix}posts WHERE post_type = 'shop_order' AND YEAR(post_date) != YEAR(NOW()));";
  $wpdb->query($delete_query);


  /*  
    Step 4
    Now delete all woocommerce order itemmeta associated with the shop_order post type. make sure to update the prefix as defined in your wp-config.php
  */
  $delete_query = "DELETE
            FROM {$wpdb->prefix}woocommerce_order_itemmeta
            WHERE order_item_id IN
                  (SELECT order_item_id
                   FROM {$wpdb->prefix}woocommerce_order_items
                   WHERE order_id IN
                         (SELECT ID
                          FROM {$wpdb->prefix}posts
                          WHERE post_type = 'shop_order'
                            AND YEAR(post_date) != YEAR(NOW())));";
  $wpdb->query($delete_query);

  /*  
    Step 5
    Now delete all woocommerce order items associated with the shop_order post type. make sure to update the prefix as defined in your wp-config.php
  */
  $delete_query = "DELETE
            FROM {$wpdb->prefix}woocommerce_order_items
            WHERE order_id IN
                  (SELECT ID
                   FROM {$wpdb->prefix}posts
                   WHERE post_type = 'shop_order'
                     AND YEAR(post_date) != YEAR(NOW()));";
  $wpdb->query($delete_query);


  /*  
    Step 6
    Now delete all woocommerce order from posts table using the post_type condition. make sure to update the prefix as defined in your wp-config.php
  */
  $delete_query = "DELETE
            FROM {$wpdb->prefix}posts
            WHERE post_type = 'shop_order'
              AND YEAR(post_date) != YEAR(NOW());";
  $wpdb->query($delete_query);
}
add_action('init', 'geeker_delete_woocommerce_old_shop_order_callback');
?>

Leave a Comment