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

<!--
Tab options on this page. Append to the .md URL to filter:
  ?tabs=<id,...>   select specific tabs (e.g. ?tabs=nodejs,shell)
  ?allTabs=true    include every tab
  (no param)       default: one tab per tabset

Available tabs:
  other tabs: mongos, mongod
-->

# db.killOp() (mongosh method)

## Description

Terminates an operation as specified by the operation ID. 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 [`db.killOp()`](https://www.mongodb.com/docs/manual/reference/method/db.killop.md#mongodb-method-db.killOp) method has the following parameter:

| Parameter | Type | Description |
| --- | --- | --- |
| `op` | number | An operation ID. |

**Warning:**

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

## Compatibility

This method 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:**

MongoDB Atlas limits the use of this method to the MongoDB user who ran the operation. For information on Atlas support for all commands, 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

## Sharded Cluster

### Kill Read Operations

The [`db.killOp()`](https://www.mongodb.com/docs/manual/reference/method/db.killop.md#mongodb-method-db.killOp) method can be run on a [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#mongodb-binary-bin.mongos) and can kill queries (read operations) that are running on more than one shard in a cluster.

For example, to kill a query operation on a sharded cluster:

### From the mongos Instance

1. On the **same** [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#mongodb-binary-bin.mongos) where the client issued the query, find the opid of the query operation to kill by running the aggregation pipeline [`$currentOp`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/currentop.md#mongodb-pipeline-pipe.-currentOp) with the `localOps:
   true`:

   ```javascript
   use admin
   db.aggregate( [
      { $currentOp : { allUsers: true, localOps: true } },
      { $match : <filter condition> } // Optional.  Specify the condition to find the op.
                                      // e.g. { op: "getmore", "command.collection": "someCollection" }
   ] )
   ```

   **Important:**

   You must issue this aggregation operation on the **same** [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#mongodb-binary-bin.mongos) where the client issued the query.

2. Once you find the query operation to kill, issue [`db.killOp()`](https://www.mongodb.com/docs/manual/reference/method/db.killop.md#mongodb-method-db.killOp) with the opid on the [`mongos`:](https://www.mongodb.com/docs/manual/reference/program/mongos.md#mongodb-binary-bin.mongos)

   ```javascript
   db.killOp(<opid of the query to kill>)
   ```

**See also:**

The `localOps` parameter in [`$currentOp`.](https://www.mongodb.com/docs/manual/reference/operator/aggregation/currentop.md#mongodb-pipeline-pipe.-currentOp)

### Kill Write Operations

Within a Session

MongoDB drivers associate all operations with a [server session](https://www.mongodb.com/docs/manual/reference/server-sessions.md#std-label-server-sessions), with the exception of unacknowledged writes.

If the write operation is associated with a session, you can use the [`killSessions`](https://www.mongodb.com/docs/manual/reference/command/killsessions.md#mongodb-dbcommand-dbcmd.killSessions) command on the [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#mongodb-binary-bin.mongos) to kill the write operation across shards.

1. Run the aggregation pipeline [`$currentOp`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/currentop.md#mongodb-pipeline-pipe.-currentOp) on the [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#mongodb-binary-bin.mongos) to find the `lsid` (logical session id).

   ```javascript
   use admin
   db.aggregate( [
      { $currentOp : { allUsers: true, localOps: true } },
      { $match : <filter condition> } // Optional.  Specify the condition to find the op.
                                      // e.g. { "op" : "update", "ns": "mydb.someCollection" }
   ] )
   ```

   1. Using the returned `lsid` information, issue the [`killSessions`](https://www.mongodb.com/docs/manual/reference/command/killsessions.md#mongodb-dbcommand-dbcmd.killSessions) command on the [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#mongodb-binary-bin.mongos) to kill the operation on the shards.

      ```javascript
      db.adminCommand( { killSessions: [
         { "id" : UUID("80e48c5a-f7fb-4541-8ac0-9e3a1ed224a4"), "uid" : BinData(0,"47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=") }
      ] } )
      ```

Without a Session

If the write operation is **not** associated with a session, you must find and kill the operation on all the shards associated with the write.

1. From a [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#mongodb-binary-bin.mongos), run the aggregation pipeline [`$currentOp`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/currentop.md#mongodb-pipeline-pipe.-currentOp) to find the opid(s) of the query operation on the shards:

   ```javascript
   use admin
   db.aggregate( [
      { $currentOp : { allUsers: true } },
      { $match : <filter condition> } // Optional.  Specify the condition to find the op.
   ] )
   ```

   When run on a [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#mongodb-binary-bin.mongos), [`$currentOp`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/currentop.md#mongodb-pipeline-pipe.-currentOp) returns the opids in the format of `"<shardName>:<opid on that
   shard>"`; e.g.

   ```javascript
   {
      "shard" : "shardB",
      ..
      "opid" : "shardB:79214",
      ...
   },
   {
      "shard" : "shardA",
      ..
      "opid" : "shardA:100913",
      ...
   },
   ```

2. Using the opid information, issue [`db.killOp()`](https://www.mongodb.com/docs/manual/reference/method/db.killop.md#mongodb-method-db.killOp) on the [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#mongodb-binary-bin.mongos) to kill the operation on the shards.

   ```javascript
   db.killOp("shardB:79014");
   db.killOp("shardA:100813");
   ```

## 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.

**See also:**

[`$currentOp`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/currentop.md#mongodb-pipeline-pipe.-currentOp)
