6

If I know the database server name, instance name and the SQL Server job name, how to delete a SQL Server job by its name in a simple way? I am writing scripts which will be called by sqlcmd to delete SQL jobs.

Appreciate if anyone could show me a sample? :-)

thanks in advance, George

George2
  • 44,761
  • 110
  • 317
  • 455

3 Answers3

16
USE msdb;

GO

EXEC sp_delete_job
    @job_name = N'NightlyBackups' ;

GO
Oleks
  • 31,955
  • 11
  • 77
  • 132
Sergey Olontsev
  • 1,084
  • 7
  • 19
3

You're looking for sp_delete_job:

[srv].[master].[dbo].sp_delete_job @job_name = 'MyJob'

So this four part name only works with linked servers. Otherwise, you'll have to connect to the server, and run that command against it (with everything right of [dbo]..

Eric
  • 92,005
  • 12
  • 114
  • 115
2

It's worth noting that you can just use SSMS, choose the job, right-click and pick "Delete", and then use the Script button at the top of the dialog box to generate a script like the ones suggested here.

Paul Sasik
  • 79,492
  • 20
  • 149
  • 189
Rob Farley
  • 15,625
  • 5
  • 44
  • 58