Page 1 of 1

SQL query to delete unsued templates

Posted: Tue Apr 28, 2009 2:00 pm
by bigbangtech
Does anyone have a short SQL query that would find and delete any product templates that are not associated with a product?

We imported a store with 1000's of items, deleted a great deal of the items and are now stuck with 100's of templates that we need to wade through before we get to the correct one.

Re: SQL query to delete unsued templates

Posted: Tue Apr 28, 2009 3:39 pm
by nickc

Code: Select all

SELECT * FROM ac_ProductTemplates
WHERE ProductTemplateId NOT IN
( SELECT DISTINCT ProductTemplateId FROM ac_InputFields where InputFieldId IN
  ( SELECT DISTINCT InputFieldId FROM ac_ProductTemplateFields )
)
Change the initial "SELECT * FROM" to "DELETE" after vetting the results of the select.
All after backing up, of course :)

Re: SQL query to delete unsued templates

Posted: Wed Apr 29, 2009 9:16 am
by bigbangtech
Thanks Nick, that worked like a charm!

I do have a lot of rows still left in ac_ProductTemplateFields that aren't connected to anything though...
nickc wrote:

Code: Select all

SELECT * FROM ac_ProductTemplates
WHERE ProductTemplateId NOT IN
( SELECT DISTINCT ProductTemplateId FROM ac_InputFields where InputFieldId IN
  ( SELECT DISTINCT InputFieldId FROM ac_ProductTemplateFields )
)
Change the initial "SELECT * FROM" to "DELETE" after vetting the results of the select.
All after backing up, of course :)

Re: SQL query to delete unsued templates

Posted: Wed Apr 29, 2009 11:37 am
by nickc

Code: Select all

SELECT * FROM ac_ProductTemplateFields WHERE InputFieldId NOT IN
( SELECT DISTINCT InputFieldId FROM ac_InputFields )
Same technique to remove orphans in ac_ProductTemplateFields.