> For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

# killOp (database command)

## Definition

Terminates an operation as specified by the operation ID. [`mongosh`](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh) provides the [`db.killOp()`](https://www.mongodb.com/docs/manual/reference/method/db.killop.md#mongodb-method-db.killOp) helper. To find operations and their corresponding IDs, see [`$currentOp`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/currentop.md#mongodb-pipeline-pipe.-currentOp) or [`db.currentOp()`.](https://www.mongodb.com/docs/manual/reference/method/db.currentop.md#mongodb-method-db.currentOp)

The `killOp` command must be run against the `admin` database.

To run killOp, use the [`db.runCommand( { <command> } )`](https://www.mongodb.com/docs/manual/reference/method/db.runcommand.md#mongodb-method-db.runCommand) method.

## Compatibility

This command is available in deployments hosted in the following environments:

- [MongoDB Atlas](https://www.mongodb.com/docs/atlas): The fully managed service for MongoDB deployments in the cloud

**Important:**

This command has limited support in all MongoDB Atlas clusters. For more information, see [Unsupported Commands.](https://www.mongodb.com/docs/atlas/unsupported-commands/)

- [MongoDB Enterprise](https://www.mongodb.com/docs/manual/administration/install-enterprise.md#std-label-install-mdb-enterprise): The subscription-based, self-managed version of MongoDB

- [MongoDB Community](https://www.mongodb.com/docs/manual/administration/install-community.md#std-label-install-mdb-community-edition): The source-available, free-to-use, and self-managed version of MongoDB

## Syntax

The command has the following form:

```javascript
db.adminCommand(
   {
     killOp: 1,
     op: <opid>,
     comment: <any>
   }
)
```

## Command Fields

| Parameter | Type | Description |
| --- | --- | --- |
| `op` | number | An operation ID. |
| `comment` | any | Optional. A user-provided comment to attach to this command. Once set, this comment appears alongside records of this command in the following locations: [mongod log messages](https://www.mongodb.com/docs/manual/reference/log-messages.md#std-label-log-messages-ref), in the `attr.command.cursor.comment` field.; [Database profiler](https://www.mongodb.com/docs/manual/reference/database-profiler.md#std-label-profiler) output, in the [`command.comment`](https://www.mongodb.com/docs/manual/reference/database-profiler.md#mongodb-data-system.profile.command) field.; [`currentOp`](https://www.mongodb.com/docs/manual/reference/command/currentop.md#mongodb-dbcommand-dbcmd.currentOp) output, in the [`command.comment`](https://www.mongodb.com/docs/manual/reference/command/currentop.md#mongodb-data-currentOp.command) field. A comment can be any valid [BSON type](https://www.mongodb.com/docs/manual/reference/bson-types.md#std-label-bson-types) (string, integer, object, array, etc). |

**Warning:**

Terminate running operations with extreme caution. Only use [`killOp`](https://www.mongodb.com/docs/manual/reference/command/killop.md#mongodb-dbcommand-dbcmd.killOp) to terminate operations initiated by clients and *do not* terminate internal database operations.

## Behavior

Do *not* use [`killOp`](https://www.mongodb.com/docs/manual/reference/command/killop.md#mongodb-dbcommand-dbcmd.killOp) to terminate an in-progress index builds in replica sets or sharded clusters. Use [`dropIndexes`](https://www.mongodb.com/docs/manual/reference/command/dropindexes.md#mongodb-dbcommand-dbcmd.dropIndexes) on the [primary](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-primary) to drop the index. See [Stop In-Progress Index Builds.](https://www.mongodb.com/docs/manual/reference/command/dropindexes.md#std-label-dropIndexes-cmd-index-builds)

### Access Control

On systems running with [`authorization`](https://www.mongodb.com/docs/manual/reference/configuration-options.md#mongodb-setting-security.authorization), to kill operations not owned by the user, the user must have access that includes the [`killop`](https://www.mongodb.com/docs/manual/reference/privilege-actions.md#mongodb-authaction-killop) privilege action.

On [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) instances, users can kill their own operations even without the [`killop`](https://www.mongodb.com/docs/manual/reference/privilege-actions.md#mongodb-authaction-killop) privilege action.

### Sharded Cluster

The [`killOp`](https://www.mongodb.com/docs/manual/reference/command/killop.md#mongodb-dbcommand-dbcmd.killOp) command can be run on a [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#mongodb-binary-bin.mongos) and can kill queries (i.e. read operations) that span shards in a cluster. The [`killOp`](https://www.mongodb.com/docs/manual/reference/command/killop.md#mongodb-dbcommand-dbcmd.killOp) command from the [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#mongodb-binary-bin.mongos) does not propagate to the shards when the operation to be killed is a write operation.

For information on how to list sharding operations that are active on a [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#mongodb-binary-bin.mongos), see the `localOps` parameter in [`$currentOp`.](https://www.mongodb.com/docs/manual/reference/operator/aggregation/currentop.md#mongodb-pipeline-pipe.-currentOp)

For more information and examples on killing operations on a sharded cluster, see:

- [Kill Read Operations](https://www.mongodb.com/docs/manual/reference/method/db.killop.md#std-label-kill-read-ops-sharded-cluster)

- [Kill Write Operations](https://www.mongodb.com/docs/manual/reference/method/db.killop.md#std-label-kill-write-ops-sharded-cluster)

## Example

The following example uses [`killOp`](https://www.mongodb.com/docs/manual/reference/command/killop.md#mongodb-dbcommand-dbcmd.killOp) to target the running operation with opid `3478`.

```javascript
db.adminCommand( { "killOp": 1, "op": 3478 } )
```

The operation returns the following result:

```javascript
{ "info" : "attempting to kill op", "ok" : 1 }
```

[`killOp`](https://www.mongodb.com/docs/manual/reference/command/killop.md#mongodb-dbcommand-dbcmd.killOp) reports success if it succeeded in marking the specified operation for termination. Operations may not actually be terminated until they reach an appropriate interruption point. Use [`$currentOp`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/currentop.md#mongodb-pipeline-pipe.-currentOp) or [`db.currentOp()`](https://www.mongodb.com/docs/manual/reference/method/db.currentop.md#mongodb-method-db.currentOp) to confirm the target operation was terminated.
