Skip to content

Instantly share code, notes, and snippets.

@mikaelz
Created October 14, 2020 09:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikaelz/8bd95a1c9b984ebc2893496ef476e146 to your computer and use it in GitHub Desktop.
Save mikaelz/8bd95a1c9b984ebc2893496ef476e146 to your computer and use it in GitHub Desktop.
Delete old (not current year) WooCommerce shop orders
DELETE
FROM wp_postmeta
WHERE post_id IN (SELECT ID FROM wp_posts WHERE post_type = 'shop_order' AND YEAR(post_date) != YEAR(NOW()));
DELETE
FROM wp_commentmeta
WHERE comment_id IN (SELECT comment_ID
FROM wp_comments
WHERE comment_post_id IN
(SELECT ID FROM wp_posts WHERE post_type = 'shop_order' AND YEAR(post_date) != YEAR(NOW()))
);
DELETE
FROM wp_comments
WHERE comment_post_id IN
(SELECT ID FROM wp_posts WHERE post_type = 'shop_order' AND YEAR(post_date) != YEAR(NOW()));
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 YEAR(post_date) != YEAR(NOW())));
DELETE
FROM wp_woocommerce_order_items
WHERE order_id IN
(SELECT ID
FROM wp_posts
WHERE post_type = 'shop_order'
AND YEAR(post_date) != YEAR(NOW()));
DELETE
FROM wp_wcpdf_invoice_number
WHERE order_id IN
(SELECT ID
FROM wp_posts
WHERE post_type = 'shop_order'
AND YEAR(post_date) != YEAR(NOW()));
DELETE
FROM wp_posts
WHERE post_type = 'shop_order'
AND YEAR(post_date) != YEAR(NOW());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment