remove maintenance plan I have the following error while trying to delete some old Miantenance plans on the MS-SQL server:


Please help.
Ok. I discovered that the source of this problem is a Server upgrade where the server name was changed and thus the outbound SQL instance name changed.The maintenance plans are stored in the msdb system database and this is where they may be edited. So after some online search, I found this SP (which I have modified):

USE msdb go 
BEGIN TRY   
    DECLARE @errno INT, @errmsg varchar(100)    DECLARE @mpname varchar(200)

--Set the NAME of the Rogue Maintenance plan here!
SET @mpname = 'myPlanToDelete' 

BEGIN TRAN

-- Delete the JobStep Logs for this plan's jobs
-- SELECT jl.*  
DELETE dbo.sysjobstepslogs  
FROM 
    dbo.sysdtspackages90 as pk
    JOIN dbo.sysmaintplan_subplans as sb on (pk.id = sb.plan_id)
    JOIN dbo.sysjobsteps as js ON (sb.job_id = js.job_id)
    JOIN dbo.sysjobstepslogs as jl on (js.job_id = jl.step_uid)
 WHERE pk.name = @mpname

-- Delete the JobHistory for this plan's jobs
--SELECT jh.*   
DELETE dbo.sysjobhistory    
FROM 
    dbo.sysdtspackages90 as pk
    JOIN dbo.sysmaintplan_subplans as sb on (pk.id = sb.plan_id)
    JOIN dbo.sysjobhistory as jh ON (sb.job_id = jh.job_id)
WHERE pk.name = @mpname

-- Delete the Job Schedules for this plan
--SELECT js.*
DELETE dbo.sysjobschedules  
FROM 
    dbo.sysdtspackages90 as pk
    JOIN dbo.sysmaintplan_subplans as sb on (pk.id = sb.plan_id)
    JOIN dbo.sysjobschedules AS js on(sb.schedule_id = js.schedule_id)
 WHERE pk.name = @mpname

-- Delete the JobSteps for this plan's jobs
--SELECT js.*
DELETE dbo.sysjobsteps  
FROM 
    dbo.sysdtspackages90 as pk
    JOIN dbo.sysmaintplan_subplans as sb on (pk.id = sb.plan_id)
    JOIN dbo.sysjobsteps as js ON (sb.job_id = js.job_id)
WHERE pk.name = @mpname

-- Delete the Jobs for this plan
--SELECT sj.*
DELETE dbo.sysjobsteps  
FROM 
    dbo.sysdtspackages90 as pk
    JOIN dbo.sysmaintplan_subplans as sb on (pk.id = sb.plan_id)
    JOIN dbo.sysjobs as sj ON (sb.job_id = sj.job_id)
WHERE pk.name = @mpname

-- Delete the Plan LOGS for this plan
--SELECT pl.*
DELETE dbo.sysmaintplan_log 
FROM 
    dbo.sysmaintplan_log as pl
    join dbo.sysdtspackages90 as pk ON (pk.id = pl.plan_id)
WHERE pk.name = @mpname

-- Delete the SUB-Plans for this Plan
--SELECT sp.*
DELETE dbo.sysmaintplan_subplans     
FROM 
    dbo.sysmaintplan_subplans as sp
    join dbo.sysdtspackages90 as pk ON (pk.id = sp.plan_id)
WHERE pk.name = @mpname

-- Delete the PLAN
-- SELECT * 
DELETE dbo.sysdtspackages90 
FROM dbo.sysdtspackages90
WHERE name = @mpname

-- All done!
COMMIT WORK

END TRY
BEGIN CATCH
    SELECT @errno = ERROR_NUMBER() ,@errmsg = ERROR_MESSAGE()
    IF (@@TRANCOUNT &gt; 0)
    BEGIN
        ROLLBACK TRAN
    END
    RAISERROR(@errmsg,16,127)
END CATCH
GO


Thanks.