Skip to content

Instantly share code, notes, and snippets.

@aslamdoctor
Last active July 4, 2024 22:10
Show Gist options
  • Save aslamdoctor/d5fdb653106222d088a4e89e4459c52d to your computer and use it in GitHub Desktop.
Save aslamdoctor/d5fdb653106222d088a4e89e4459c52d to your computer and use it in GitHub Desktop.
WooCommerce : Delete all failed orders
-- Below queries are just to check the failed order records:
SELECT * FROM `wp_woocommerce_order_itemmeta` WHERE order_item_id IN (SELECT order_item_id FROM `wp_woocommerce_order_items` WHERE order_id IN (SELECT ID FROM wp_posts WHERE post_type = 'shop_order' AND post_status='wc-failed'))
SELECT * FROM `wp_woocommerce_order_items` WHERE order_id IN (SELECT ID FROM wp_posts WHERE post_type = 'shop_order' AND post_status='wc-failed')
SELECT * FROM wp_comments WHERE comment_type = 'order_note' AND comment_post_ID IN(SELECT ID FROM wp_posts WHERE post_type = 'shop_order' AND post_status='wc-failed')
SELECT * FROM wp_postmeta WHERE post_id IN ( SELECT ID FROM wp_posts WHERE post_type = 'shop_order' AND post_status='wc-failed')
SELECT ID FROM wp_posts WHERE post_type = 'shop_order' AND post_status='wc-failed'
-- Below are the actual queries to delete all failed orders. Make sure to run them in same order otherwise it will not work properly.
DELETE FROM `wp_woocommerce_order_itemmeta` WHERE order_item_id IN (SELECT order_item_id FROM `wp_woocommerce_order_items` WHERE order_id IN (SELECT ID FROM wp_posts WHERE post_type = 'shop_order' AND post_status='wc-failed'))
DELETE FROM `wp_woocommerce_order_items` WHERE order_id IN (SELECT ID FROM wp_posts WHERE post_type = 'shop_order' AND post_status='wc-failed')
DELETE FROM wp_comments WHERE comment_type = 'order_note' AND comment_post_ID IN(SELECT ID FROM wp_posts WHERE post_type = 'shop_order' AND post_status='wc-failed')
DELETE FROM wp_postmeta WHERE post_id IN ( SELECT ID FROM wp_posts WHERE post_type = 'shop_order' AND post_status='wc-failed')
DELETE FROM wp_posts WHERE post_type = 'shop_order' AND post_status='wc-failed'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment