Docs Menu

db.killOp()

On this page

  • Description
  • Sharded Cluster
  • Access Control
db.killOp(opid)

Terminates an operation as specified by the operation ID. To find operations and their corresponding IDs, see $currentOp or db.currentOp().

The 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() to terminate operations initiated by clients and do not terminate internal database operations.

Starting in MongoDB 4.0

The db.killOp() method can be run on a 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 MongoDB 4.0+ sharded cluster:

For MongoDB 3.6 and earlier

To kill a query running on 3.6 (or earlier) sharded clusters, you must kill the operation on all the shards associated with the query.

  1. From a mongos, run the aggregation pipeline $currentOp to find the opid(s) of the query operation on the shards:

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

    When run on a mongos, $currentOp returns the opids in the format of "<shardName>:<opid on that shard>"; e.g.

    {
    "shard" : "shardB",
    ..
    "opid" : "shardB:79014",
    ...
    },
    {
    "shard" : "shardA",
    ..
    "opid" : "shardA:100813",
    ...
    },
  2. Using the opid information, issue db.killOp() on the mongos to kill the operation on the shards.

    db.killOp("shardB:79014");
    db.killOp("shardA:100813");
Within a Session

Starting in MongoDB 3.6, MongoDB drivers associate all operations with a server session, with the exception of unacknowledged writes.

If the write operation is associated with a session, you can use the killSessions command on the mongos to kill the write operation across shards.

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, run the aggregation pipeline $currentOp to find the opid(s) of the query operation on the shards:

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

    When run on a mongos, $currentOp returns the opids in the format of "<shardName>:<opid on that shard>"; e.g.

    {
    "shard" : "shardB",
    ..
    "opid" : "shardB:79214",
    ...
    },
    {
    "shard" : "shardA",
    ..
    "opid" : "shardA:100913",
    ...
    },
  2. Using the opid information, issue db.killOp() on the mongos to kill the operation on the shards.

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

On systems running with authorization, to kill operations not owned by the user, the user must have access that includes the killop privilege action.

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

Tip
See also:
Give Feedback
© 2021 MongoDB, Inc.

About

  • Careers
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2021 MongoDB, Inc.