Delete WooCommerce Products by Import ID with SQL
Learn how to bulk delete WooCommerce products imported through
WP All Import, using a targeted SQL query based on the
import_id.
What this SQL query does
Managing thousands of WooCommerce products often involves bulk imports with
WP All Import. When you need to remove all products created by a specific import,
you can target the related import_id directly in the database.
The query collects the products and variations from the selected import, then removes their related database records, including post meta, term relationships, lookup records, and optionally linked attachments.
1) Why delete products by Import ID?
Deleting products by import_id is often safer and faster than manually removing products one by one.
- You ran a test import on staging and need to undo it.
- You accidentally imported duplicate WooCommerce products.
- You want to replace an entire supplier product feed.
- You need to remove only the products from one specific import without affecting the rest of the catalogue.
wp_pmxi_posts table.
2) Before running the query
Because this query performs a destructive database action, you should always validate everything first.
- Create a full database backup.
- Test the query on a staging environment first.
- Confirm the correct
@IMPORT_ID. - Replace the prefix
A9B2c1u_with your real WordPress database table prefix. - Do not run this on production unless you are completely sure about the result.
3) SQL query to delete products from a specific import
Replace @IMPORT_ID with the ID of the import you want to clean up and replace
A9B2c1u_ with your actual WordPress database table prefix.
-- Set your specific import ID here
SET @IMPORT_ID = 1;
-- 1) Collect ALL products & variations from this import
CREATE TEMPORARY TABLE to_del (id BIGINT PRIMARY KEY);
INSERT INTO to_del
SELECT p.ID
FROM A9B2c1u_posts p
JOIN A9B2c1u_pmxi_posts x ON x.post_id = p.ID
WHERE x.import_id = @IMPORT_ID
AND p.post_type IN ('product','product_variation');
-- 2) Optional but recommended: delete related attachments
CREATE TEMPORARY TABLE to_del_att (id BIGINT PRIMARY KEY);
INSERT INTO to_del_att
SELECT a.ID
FROM A9B2c1u_posts a
WHERE a.post_type = 'attachment'
AND a.post_parent IN (SELECT id FROM to_del);
DELETE pm FROM A9B2c1u_postmeta pm
JOIN to_del_att da ON pm.post_id = da.id;
DELETE tr FROM A9B2c1u_term_relationships tr
JOIN to_del_att da ON tr.object_id = da.id;
DELETE a FROM A9B2c1u_posts a
JOIN to_del_att da ON a.ID = da.id;
DROP TABLE to_del_att;
-- 3) Delete products/variations + all related records
DELETE pm FROM A9B2c1u_postmeta pm
JOIN to_del d ON pm.post_id = d.id;
DELETE tr FROM A9B2c1u_term_relationships tr
JOIN to_del d ON tr.object_id = d.id;
DELETE wcl FROM A9B2c1u_wc_product_meta_lookup wcl
JOIN to_del d ON wcl.product_id = d.id;
DELETE p FROM A9B2c1u_posts p
JOIN to_del d ON p.ID = d.id;
DELETE x FROM A9B2c1u_pmxi_posts x
JOIN to_del d ON x.post_id = d.id;
DROP TABLE to_del;
4) How this query works
SET @IMPORT_ID = 1;, you define which WP All Import batch you want to clean up.
product and product_variation connected to that import.
postmeta, term_relationships and wc_product_meta_lookup.
posts table.
5) How to find the correct Import ID
The import_id is the ID of the import created by WP All Import.
You can find it from the WordPress admin or directly in the database.
- Go to WP All Import → Manage Imports and check the import ID.
- Alternatively, check the
wp_pmxi_importstable in the database. - Before deleting anything, run a SELECT query against
wp_pmxi_poststo confirm that the import contains the correct products.
SET @IMPORT_ID = 1;
SELECT p.ID, p.post_title, p.post_type, p.post_status
FROM A9B2c1u_posts p
JOIN A9B2c1u_pmxi_posts x ON x.post_id = p.ID
WHERE x.import_id = @IMPORT_ID
AND p.post_type IN ('product','product_variation')
ORDER BY p.ID DESC;
6) Where to run the query
You can execute this query in different ways depending on your level of database access.
- phpMyAdmin: from the SQL tab of your database.
- Adminer: if you use a lightweight database management tool.
- WP-CLI: with
wp db query, if you have SSH access. - MySQL client: directly from the terminal for more advanced control.
7) Best practices
- Always test the query on staging before running it on production.
- Double-check the value of
@IMPORT_ID. - Create a full database backup before execution.
- Run the SELECT preview query first to see which products will be deleted.
- Make sure the database table prefix is correct in every table name.
- After execution, clear cache and review the WooCommerce product list.
8) Frequently Asked Questions
No. The query targets only products and variations linked to the selected import_id.
Yes. This example uses A9B2c1u_. If your prefix is wp_,
you must replace it in every table name.
Yes. The query targets both product and product_variation post types.
The query includes an optional step for attachments that have one of the deleted products as their post_parent.
Not easily. That is why you should always create a full database backup before executing the query.
9) Support
For any issue or question related to WooCommerce SQL snippets, you can contact technical support.