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

# killCursors (database command)

## Definition

Kills the specified cursor or cursors for a collection. MongoDB drivers use the [`killCursors`](https://www.mongodb.com/docs/manual/reference/command/killCursors.md#mongodb-dbcommand-dbcmd.killCursors) command as part of the client-side cursor implementation.

**Warning:**

Applications typically should not run the `killCursors` command directly. Instead, let the driver automatically handle cursor management.

The `killCursors` command must be run against the database of the collection whose cursors you wish to kill.

To run killCursors, use the [`db.runCommand( { <command> } )`](https://www.mongodb.com/docs/manual/reference/method/db.runCommand.md#mongodb-method-db.runCommand) method.

## Compatibility

This command 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

**Note:**

This command is supported in all MongoDB Atlas clusters. 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

## Syntax

The command has the following syntax:

```javascript
db.runCommand(
   {
     killCursors: <collection>,
     cursors: [ <cursor id1>, ... ], comment: <any>
   }
)
```

## Command Fields

The command takes the following fields:

| Field | Type | Description |
| --- | --- | --- |
| `killCursors` | string | The name of the collection. |
| `cursors` | array | The ids of the cursors to kill. |
| `comment` | 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: [mongod log messages](https://www.mongodb.com/docs/manual/reference/log-messages.md#std-label-log-messages-ref), in the `attr.command.cursor.comment` field.; [Database profiler](https://www.mongodb.com/docs/manual/reference/database-profiler.md#std-label-profiler) output, in the [`command.comment`](https://www.mongodb.com/docs/manual/reference/database-profiler.md#mongodb-data-system.profile.command) field.; [`currentOp`](https://www.mongodb.com/docs/manual/reference/command/currentOp.md#mongodb-dbcommand-dbcmd.currentOp) output, in the [`command.comment`](https://www.mongodb.com/docs/manual/reference/command/currentOp.md#mongodb-data-currentOp.command) field. A comment can be any valid [BSON type](https://www.mongodb.com/docs/manual/reference/bson-types.md#std-label-bson-types) (string, integer, object, array, etc). |

## Required Access

### Kill Own Cursors

Users can always kill their own cursors regardless of whether they have the [`killCursors`](https://www.mongodb.com/docs/manual/reference/privilege-actions.md#mongodb-authaction-killCursors) privilege. Cursors are associated with the users at the time of cursor creation.

### Kill Any Cursor

If a user has the [`killAnyCursor`](https://www.mongodb.com/docs/manual/reference/privilege-actions.md#mongodb-authaction-killAnyCursor) privilege, they can kill cursors created by any user.

## `killCursors` and Transactions

You cannot specify the [`killCursors`](https://www.mongodb.com/docs/manual/reference/command/killCursors.md#mongodb-dbcommand-dbcmd.killCursors) command as the first operation in a [transaction.](https://www.mongodb.com/docs/manual/core/transactions.md#std-label-transactions)

Additionally, if you run the `killCursors` command within a transaction, the server immediately stops the specified cursors. It does **not** wait for the transaction to commit.

## Example

Consider the following [`find`](https://www.mongodb.com/docs/manual/reference/command/find.md#mongodb-dbcommand-dbcmd.find) operation on the `test.restaurants` collection:

```javascript
use test
db.runCommand(
   { find: "restaurants",
     filter: { stars: 5 },
     projection: { name: 1, rating: 1, address: 1 },
     sort: { name: 1 },
     batchSize: 5
   }
)
```

which returns the following:

```javascript
{
   "waitedMS" : Long(0),
   "cursor" : {
      "firstBatch" : [
         {
            "_id" : ObjectId("57506d63f578028074723dfd"),
            "name" : "Cakes and more"
         },
         {
            "_id" : ObjectId("57506d63f578028074723e0b"),
            "name" : "Pies and things"
         },
         {
            "_id" : ObjectId("57506d63f578028074723e1d"),
            "name" : "Ice Cream Parlour"
         },
         {
            "_id" : ObjectId("57506d63f578028074723e65"),
            "name" : "Cream Puffs"
         },
         {
            "_id" : ObjectId("57506d63f578028074723e66"),
            "name" : "Cakes and Rolls"
         }
      ],
      "id" : Long("18314637080"),
      "ns" : "test.restaurants"
   },
   "ok" : 1
}
```

To kill this cursor, use the [`killCursors`](https://www.mongodb.com/docs/manual/reference/command/killCursors.md#mongodb-dbcommand-dbcmd.killCursors) command.

```javascript
use test

db.runCommand( { killCursors: "restaurants", cursors: [ Long("18314637080") ] } )
```

[`killCursors`](https://www.mongodb.com/docs/manual/reference/command/killCursors.md#mongodb-dbcommand-dbcmd.killCursors) returns the following operation details:

```javascript
{
   "cursorsKilled" : [
      Long("18314637080")
   ],
   "cursorsNotFound" : [ ],
   "cursorsAlive" : [ ],
   "cursorsUnknown" : [ ],
   "ok" : 1
}
```
