- Reference >
mongoshMethods >- Database Methods >
- db.killOp()
db.killOp()¶
On this page
Description¶
-
db.killOp(opid)¶ Terminates an operation as specified by the operation ID. To find operations and their corresponding IDs, see
$currentOpordb.currentOp().The
db.killOp()method has the following parameter:Parameter Type Description opnumber 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.
Sharded Cluster¶
Kill Read Operations¶
- Starting in MongoDB 4.0
The
db.killOp()method can be run on amongosand 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:
- From the mongos Instance
- From a shard member
On the same
mongoswhere the client issued the query, find the opid of the query operation to kill by running the aggregation pipeline$currentOpwith thelocalOps: true:Important
You must issue this aggregation operation on the same
mongoswhere the client issued the query.Once you find the query operation to kill, issue
db.killOp()with the opid on themongos:
See also
The
localOpsparameter in$currentOp.Alternatively, you can find and kill the read operation from a shard member where the operation is running. MongoDB 4.0+ propagates the kill operation to the other shards and
mongosinstances:On one of the shards where the operation is running, find the opid of the query operation to kill:
Once you find the query operation to kill, issue
db.killOp()with the opid on the shard member:MongoDB 4.0+ propagates the kill operation to the other shards and
mongosinstances.
- 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.
From a
mongos, run the aggregation pipeline$currentOpto find the opid(s) of the query operation on the shards:When run on a
mongos,$currentOpreturns the opids in the format of"<shardName>:<opid on that shard>"; e.g.Using the opid information, issue
db.killOp()on themongosto kill the operation on the shards.
Kill Write Operations¶
- 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
killSessionscommand on themongosto kill the write operation across shards.- MongoDB 4.0+
- MongoDB 3.6
Run the aggregation pipeline
$currentOpon themongosto find thelsid(logical session id).Using the returned
lsidinformation, issue thekillSessionscommand on themongosto kill the operation on the shards.
Run the aggregation pipeline
$currentOpon themongosor the individual shards find thelsid(logical session id).Using the returned lsid information, issue the
killSessionscommand on themongosto kill the operation on the 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.
From a
mongos, run the aggregation pipeline$currentOpto find the opid(s) of the query operation on the shards:When run on a
mongos,$currentOpreturns the opids in the format of"<shardName>:<opid on that shard>"; e.g.Using the opid information, issue
db.killOp()on themongosto kill the operation on the shards.
Access Control¶
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.
See also