db.killOp()

Description

  • db.killOp(opid)
  • Terminates an operation as specified by the operation ID. To findoperations and their corresponding IDs, see $currentOpor db.currentOp().

The db.killOp() method has the following parameter:

ParameterTypeDescriptionopnumberAn operation ID.

Warning

Terminate running operations with extreme caution. Only usedb.killOp() to terminate operations initiated by clientsand do not terminate internal database operations.

Sharded Cluster

Kill Read Operations

  • Starting in MongoDB 4.0
  • The db.killOp() method can be run on amongos and can kill queries (read operations) that are running on morethan one shard in a cluster.

For example, to kill a query operation on a MongoDB 4.0+ shardedcluster:

  • From the mongos Instance
  • From a shard member
  • On the samemongos where the client issued thequery, find the opid of the query operation to kill by running theaggregation pipeline $currentOp with the localOps:true:
  1. use admin
  2. db.aggregate( [
  3. { $currentOp : { allUsers: true, localOps: true } },
  4. { $match : <filter condition> } // Optional. Specify the condition to find the op.
  5. // e.g. { op: "getmore", "command.collection": "someCollection" }
  6. ] )

Important

You must issue this aggregation operation on the samemongos where the client issued the query.

  • Once you find the query operation to kill, issuedb.killOp() with the opid on the mongos:
  1. db.killOp(<opid of the query to kill>)

See also

The localOps parameter in $currentOp.

Alternatively, you can find and kill the read operation from ashard member where the operation is running. MongoDB 4.0+propagates the kill operation to the other shards andmongos instances:

  • On one of the shards where the operation is running, find the opidof the query operation to kill:
  1. use admin
  2. db.aggregate( [
  3. { $currentOp : { allUsers: true } },
  4. { $match : <filter condition> } // Optional. Specify the condition to find the op.
  5. // e.g. { op: "getmore", "command.collection": "someCollection" }
  6. ] )
  • Once you find the query operation to kill, issuedb.killOp() with the opid on the shard member:
  1. db.killOp(<opid of the query to kill>)

MongoDB 4.0+ propagates the kill operation to theother shards and mongos instances.

  • For MongoDB 3.6 and earlier
  • To kill a query running on 3.6 (or earlier) sharded clusters, you must kill theoperation on all the shards associated with the query.

    • From a mongos, run the aggregation pipeline$currentOp to find the opid(s) of the query operation onthe shards:
  1. use admin
  2. db.aggregate( [
  3. { $currentOp : { allUsers: true } },
  4. { $match : <filter condition> } // Optional. Specify the condition to find the op.
  5. // e.g. { op: "getmore", "command.collection": "someCollection" }
  6. ] )

When run on a mongos, $currentOpreturns the opids in the format of "<shardName>:<opid on thatshard>"; e.g.

  1. {
  2. "shard" : "shardB",
  3. ..
  4. "opid" : "shardB:79014",
  5. ...
  6. },
  7. {
  8. "shard" : "shardA",
  9. ..
  10. "opid" : "shardA:100813",
  11. ...
  12. },
  • Using the opid information, issue db.killOp() on themongos to kill the operation on the shards.
  1. db.killOp("shardB:79014");
  2. db.killOp("shardA:100813");

Kill Write Operations

  • Within a Session
  • Starting in MongoDB 3.6, MongoDB drivers associate all operationswith a server session, with theexception of unacknowledged writes.

If the write operation is associated with a session, you can use thekillSessions command on the mongos tokill the write operation across shards.

  • MongoDB 4.0+
  • MongoDB 3.6
  • Run the aggregation pipeline $currentOp onthe mongos to find thelsid (logical session id).
  1. use admin
  2. db.aggregate( [
  3. { $currentOp : { allUsers: true, localOps: true } },
  4. { $match : <filter condition> } // Optional. Specify the condition to find the op.
  5. // e.g. { "op" : "update", "ns": "mydb.someCollection" }
  6. ] )
  • Using the returned lsid information, issue thekillSessions command on themongos to kill the operation on the shards.
  1. db.adminCommand( { killSessions: [
  2. { "id" : UUID("80e48c5a-f7fb-4541-8ac0-9e3a1ed224a4"), "uid" : BinData(0,"47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=") }
  3. ] } )
  • Run the aggregation pipeline $currentOp onthe mongos or the individual shards find thelsid (logical session id).
  1. use admin
  2. db.aggregate( [
  3. { $currentOp : { allUsers: true } },
  4. { $match : <filter condition> } // Optional. Specify the condition to find the op.
  5. // e.g. { "op" : "update", "ns": "mydb.someCollection" }
  6. ] )
  • Using the returned lsid information, issue thekillSessions command on themongos to kill the operation on theshards.
  1. db.adminCommand( { killSessions: [
  2. { "id" : UUID("80e48c5a-f7fb-4541-8ac0-9e3a1ed224a4"), "uid" : BinData(0,"47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=") }
  3. ] } )
  • Without a Session
  • If the write operation is not associated with a session, you must find and kill theoperation on all the shards associated with the write.

    • From a mongos, run the aggregation pipeline$currentOp to find the opid(s) of the query operation onthe shards:
  1. use admin
  2. db.aggregate( [
  3. { $currentOp : { allUsers: true } },
  4. { $match : <filter condition> } // Optional. Specify the condition to find the op.
  5. ] )

When run on a mongos, $currentOpreturns the opids in the format of "<shardName>:<opid on thatshard>"; e.g.

  1. {
  2. "shard" : "shardB",
  3. ..
  4. "opid" : "shardB:79214",
  5. ...
  6. },
  7. {
  8. "shard" : "shardA",
  9. ..
  10. "opid" : "shardA:100913",
  11. ...
  12. },
  • Using the opid information, issue db.killOp() on themongos to kill the operation on the shards.
  1. db.killOp("shardB:79014");
  2. db.killOp("shardA:100813");

Access Control

On systems running with authorization, to killoperations not owned by the user, the user must have access thatincludes the killop privilege action.

Changed in version 3.2.9: On mongod instances, users can kill their own operationseven without the killop privilege action.

See also

$currentOp