Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 527633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:54:14+00:00 2026-05-13T08:54:14+00:00

I want to drop all the SQL Agent Jobs which are not currently running

  • 0

I want to drop all the SQL Agent Jobs which are not currently running and have description of ‘xxxx’. How can this be done in script?

As of now i got this done as below and sure there would be better way to do.

DECLARE @job_owner_name VARCHAR(100)
DECLARE @MaxJobs INT
declare @RowCnt INT
DECLARE @jobId NVARCHAR(36)
select @RowCnt = 1


SET @job_owner_name = ''
IF OBJECT_ID('tempdb..#xp_results') IS NOT NULL 
DROP TABLE #xp_results

Declare  @xp_results TABLE
( job_id UNIQUEIDENTIFIER NOT NULL,
  last_run_date INT NOT NULL,
  last_run_time INT NOT NULL,
  next_run_date INT NOT NULL,
  next_run_time INT NOT NULL,
  next_run_schedule_id INT NOT NULL,
  requested_to_run INT NOT NULL,
  request_source INT NOT NULL,
  request_source_id SYSNAME COLLATE database_default NULL,
  running INT NOT NULL,
  current_step INT NOT NULL,
  current_retry_attempt INT NOT NULL,
  job_state INT NOT NULL
)      

INSERT  INTO @xp_results 
EXEC master.dbo.xp_sqlagent_enum_jobs 1, ''

Declare @jobs TABLE (rownum int IDENTITY (1, 1) Primary key NOT NULL ,
                job_id UNIQUEIDENTIFIER NOT NULL)
INSERT INTO @jobs 
SELECT  rj.job_id
FROM    @xp_results rj
        INNER JOIN msdb.dbo.sysjobs sj ON sj.job_id = rj.job_id
WHERE   rj.running <>1 
        AND sj.[description] = 'MasterJobHanlder'

SELECT @MaxJobs = COUNT(*) FROM @jobs
WHILE @RowCnt <= @MaxJobs
BEGIN
SELECT @jobId = job_id FROM @jobs WHERE rownum = @rowcnt
SELECT @jobID
EXEC msdb.dbo.sp_delete_job @job_id = @jobid, @delete_unused_schedule=1
Select @RowCnt = @RowCnt + 1
END

any further ideas please

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:54:14+00:00Added an answer on May 13, 2026 at 8:54 am

    I have not tested this much, but something like this seems like it would work:

    USE msdb
    
    DECLARE @deleteCmd nvarchar(max);
    SET @deleteCmd = '';
    
    SELECT @deleteCmd =  @deleteCmd 
      + ( 'exec dbo.sp_delete_job @job_name = ''' 
      + replace( j.name, '''', '''''' ) + '''; ' 
      + char(10) )
    FROM dbo.sysjobs j
    WHERE j.[description] like '%foo%'
    AND not exists (
        SELECT 1
        FROM dbo.sysjobactivity a
        WHERE a.job_id = j.job_id
          AND a.start_execution_date IS NOT NULL
          AND a.stop_execution_date IS NULL
    )
    
    PRINT @deleteCmd --print for troubleshooting
    
    -- really: EXECUTE( @deleteCmd )
    

    Credit: http://timlaqua.com/2009/05/a-cleaner-way-to-detect-the-state-of-a-sql-server-agent-job-in-sql-server-2005/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a TcxDBColorCombobox and I want the drop down to not include the
I have a server in my network for which I want to DROP outbound
I have the hg shelve (not attic) extension installed, and I want to drop
I have a database running under Sql server 2005 with merge replication. I want
Why does SQL 2008 all of a sudden want to drop my tables when
Afternoon All, I have a dropdown list which extracts data from my SQL database
I want to drop all the schemas in a mysql db, and I don't
I want to drop a now supposedly redundant file in SQL Server (2005), but
I have a large multidimensional array and I basically want to drop the first
I want to drop a lot of SPs from my SQL Server database. However,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.