delete from msdb..sysmaintplan_log
where plan_id not in (
select plan_id from msdb..sysmaintplan_plans
)
delete from msdb..sysmaintplan_subplans
where plan_id not in (
select plan_id from msdb..sysmaintplan_plans
delete from msdb..sysmaintplan_log
where plan_id not in (
select plan_id from msdb..sysmaintplan_plans
)
delete from msdb..sysmaintplan_subplans
where plan_id not in (
select plan_id from msdb..sysmaintplan_plans