Skip to content

Instantly share code, notes, and snippets.

@nunomorgadinho
Created April 23, 2021 08:08
Show Gist options
  • Save nunomorgadinho/ede41db70ed40c358c9c95073bbd49d9 to your computer and use it in GitHub Desktop.
Save nunomorgadinho/ede41db70ed40c358c9c95073bbd49d9 to your computer and use it in GitHub Desktop.
SQL query to remove old orders in WooCommerce
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_date < '2017-01-01'
)
)
DELETE
FROM wp_woocommerce_order_items
WHERE order_id IN (
SELECT ID
FROM wp_posts
WHERE post_date <= '2017-01-01'
)
DELETE
FROM wp_comments
WHERE comment_type = 'order_note'
AND comment_post_ID IN (
SELECT ID
FROM wp_posts
WHERE post_date <= '2017-01-01'
)
DELETE
FROM wp_postmeta
WHERE post_id IN (
SELECT ID
FROM wp_posts
WHERE post_type = 'shop_order'
AND post_date <= '2017-01-01'
)
DELETE
FROM wp_posts
WHERE post_type = 'shop_order'
AND post_date <= '2017-01-01'
@nunomorgadinho
Copy link
Author

Delete the children items first and then remove the parents last. DO NOT RUN THIS WITHOUT TESTING FIRST. I accept no responsibility for lost data.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment