--
-- Clean up orphaned Maintenance Plan subplans and their logs
-- Use this if you get a conflict error deleting a job
--

use [msdb]
GO

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

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