---
title: "Remove-DbaAgentJob"
description: "Removes SQL Server Agent jobs from one or more instances with options to preserve history and schedules."
url: "https://dbatools.io/Remove-DbaAgentJob/"
availability: "Windows, Linux, macOS"
tags: ["Agent", "Job"]
author: "Sander Stad (@sqlstad, sqlstad.nl)"
source: "https://github.com/dataplat/dbatools/blob/master/public/Remove-DbaAgentJob.ps1"
last_updated: "2024-01-01"
---

# Remove-DbaAgentJob

## Synopsis

Removes SQL Server Agent jobs from one or more instances with options to preserve history and schedules.

## Description

Removes SQL Server Agent jobs from the target instances using the sp_delete_job system stored procedure. By default, both job history and unused schedules are deleted along with the job itself. You can optionally preserve job execution history for compliance or troubleshooting purposes, and keep unused schedules that might be reused for other jobs. This function is commonly used when decommissioning applications, cleaning up test environments, or removing obsolete maintenance jobs during server consolidation projects.

## Syntax

```powershell
Remove-DbaAgentJob
    [[-SqlInstance] <DbaInstanceParameter[]>]
    [[-SqlCredential] <PSCredential>]
    [[-Job] <Object[]>]
    [-KeepHistory]
    [-KeepUnusedSchedule]
    [[-InputObject] <Job[]>]
    [-EnableException]
    [-WhatIf]
    [-Confirm]
    [<CommonParameters>]

```

## Examples

### Example 1: Removes the job from the instance with the name Job1

```powershell
PS C:\> Remove-DbaAgentJob -SqlInstance sql1 -Job Job1
```

### Example 2: Removes the job but keeps the history

```powershell
PS C:\> GetDbaAgentJob -SqlInstance sql1 -Job Job1 | Remove-DbaAgentJob -KeepHistory
```

### Example 3: Removes the job but keeps the unused schedules

```powershell
PS C:\> Remove-DbaAgentJob -SqlInstance sql1 -Job Job1 -KeepUnusedSchedule
```

### Example 4: Removes the job from multiple servers

```powershell
PS C:\> Remove-DbaAgentJob -SqlInstance sql1, sql2, sql3 -Job Job1
```

### Optional Parameters

##### -SqlInstance

The target SQL Server instance or instances. This can be a collection and receive pipeline input to allow the function to be executed against multiple SQL Server instances.

| Property | Value |
| --- | --- |
| Alias |  |
| Required | False |
| Pipeline | false |
| Default Value |  |

##### -SqlCredential

Login to the target instance using alternative credentials. Accepts PowerShell credentials (Get-Credential).  
Windows Authentication, SQL Server Authentication, Active Directory - Password, and Active Directory - Integrated are all supported.  
For MFA support, please use Connect-DbaInstance.

| Property | Value |
| --- | --- |
| Alias |  |
| Required | False |
| Pipeline | false |
| Default Value |  |

##### -Job

Specifies the name of the SQL Server Agent job to remove. Accepts one or more job names.  
Use this when you know the specific job names you want to delete, rather than piping job objects.

| Property | Value |
| --- | --- |
| Alias |  |
| Required | False |
| Pipeline | false |
| Default Value |  |

##### -KeepHistory

Preserves job execution history in the msdb.dbo.sysjobhistory tables when removing the job.  
Use this when you need to retain audit trails or troubleshooting information for compliance or analysis purposes.

| Property | Value |
| --- | --- |
| Alias |  |
| Required | False |
| Pipeline | false |
| Default Value | False |

##### -KeepUnusedSchedule

Preserves job schedules that aren't used by other jobs when removing this job.  
Use this when you plan to reuse the schedule for new jobs or want to maintain schedule definitions for documentation purposes.

| Property | Value |
| --- | --- |
| Alias |  |
| Required | False |
| Pipeline | false |
| Default Value | False |

##### -InputObject

Accepts SQL Server Agent job objects from the pipeline, typically from Get-DbaAgentJob.  
Use this approach when you need to filter jobs with complex criteria before removal or when processing jobs from multiple instances.

| Property | Value |
| --- | --- |
| Alias |  |
| Required | False |
| Pipeline | true (ByValue) |
| Default Value |  |

##### -EnableException

By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.  
This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting.  
Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.

| Property | Value |
| --- | --- |
| Alias |  |
| Required | False |
| Pipeline | false |
| Default Value | False |

##### -WhatIf

Shows what would happen if the command were to run. No actions are actually performed.

| Property | Value |
| --- | --- |
| Alias | wi |
| Required | False |
| Pipeline | false |
| Default Value |  |

##### -Confirm

Prompts you for confirmation before executing any changing operations within the command.

| Property | Value |
| --- | --- |
| Alias | cf |
| Required | False |
| Pipeline | false |
| Default Value |  |

## Outputs

**PSCustomObject**

**Returns one object per job removed, with the following properties:**

- ComputerName: The computer name of the SQL Server instance
- InstanceName: The SQL Server instance name
- SqlInstance: The full SQL Server instance name (computer\instance)
- Name: The job name that was removed
- Status: The status of the removal operation ("Dropped" on success, or "Failed. {error message}" on failure)

---

Part of [dbatools](https://dbatools.io/), a free and open source PowerShell module for SQL Server administration. Full command index: https://dbatools.io/commands/
