killOp
Definition
killOp
Terminates an operation as specified by the operation ID.
mongosh
provides thedb.killOp()
helper. To find operations and their corresponding IDs, see$currentOp
ordb.currentOp()
.The
killOp
command must be run against theadmin
database.To run killOp, use the
db.runCommand( { <command> } )
method.
Compatibility
This command is available in deployments hosted in the following environments:
MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
Important
This command has limited support in M0, M2, M5, and M10+ clusters. For more information, see Unsupported Commands.
MongoDB Enterprise: The subscription-based, self-managed version of MongoDB
MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB
Syntax
The command has the following form:
db.adminCommand( { killOp: 1, op: <opid>, comment: <any> } )
Command Fields
Parameter | Type | Description |
---|---|---|
| number | An operation ID. |
| 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:
A comment can be any valid BSON type (string, integer, object, array, etc). |
Warning
Terminate running operations with extreme caution. Only use
killOp
to terminate operations initiated by clients
and do not terminate internal database operations.
Behavior
Do not use killOp
to terminate an in-progress index
builds in replica sets or sharded clusters. Use dropIndexes
on the primary to drop the index. See
Stop In-Progress Index Builds.
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.
On mongod
instances, users can kill their own operations
even without the killop
privilege action.
Sharded Cluster
The killOp
command can be run on
a mongos
and can kill queries (i.e. read operations)
that span shards in a cluster. The killOp
command from the
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
, see the localOps
parameter in
$currentOp
.
For more information and examples on killing operations on a sharded cluster, see:
Example
The following example uses killOp
to target
the running operation with opid 3478
.
db.adminCommand( { "killOp": 1, "op": 3478 } )
The operation returns the following result:
{ "info" : "attempting to kill op", "ok" : 1 }
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
or db.currentOp()
to confirm the
target operation was terminated.