Monday, April 20, 2009

Error when you try to delete a maintenance task SQL 2005

Have you ever had a problem deleting a maintenance task in SQL 2005? Today I am going to show you how to delete a maintenance task due to the following error:

This error is elusive, there is no problem with connection! Most likely you cannot delete the task because there are records of it's execution stored in two tables that I will introduce shortly. First let me show you a table where maintenance tasks are listed. This is the same as viewing the maintenance tasks in Management Studio except doing it with T-SQL.

SELECT*FROM MSDB.DBO.SYSDTSPACKAGES90

Now that you know ID name of the maintenance task you're trying to delete we can review which tables hold the deletion.

If you try to delete with T-SQL you will get the following error:

It means there are records in MSDB.DBO.SYSMAINTPLAN_LOG table that are preventing you from deleting the maintenance task.

In my case, there are four records in the table that belong to this maintenance task.

Now that you run a delete statement using the maintenance task ID, you will clean up first of the two references.

The other table that has reference to the maintenance task is called MSDB.DBO.SYSMAINTPLAN_SUBPLANS.

This table shows how many subplans there are in your maintenance plan. In my case it's one.

Now if you run a delete with the maintenance plan ID you're trying to get rid off, you will clean out any subplans that your maintenance task has.

Now we will run the original select statement against SYSDTSPACKAGES90 table to make sure the maintenance task is still there.

Now you can run a delete statement against the original table or delete the maintenance task in management studio and it will be able to delete.

Task is deleted!

Summary:

First of all, there are recommendations not to touch system dbs. So far this is the only way I was able to find that can get rid off rogue maintenance tasks. Until then, I will continue to use this. Always run a full backup of msdb database before performing this process. I am not responsible for your actions and use this procedure at your own risk!