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

# removeQuerySettings (database command)

## Definition

**New in version 8.0**

Deletes query settings previously added with [`setQuerySettings`.](https://www.mongodb.com/docs/manual/reference/command/setquerysettings.md#mongodb-dbcommand-dbcmd.setQuerySettings)

To delete query settings, you must provide either a [query shape](https://www.mongodb.com/docs/manual/core/query-shapes.md#std-label-query-shapes) hash string or a query shape to `removeQuerySettings`.

To find a hash string or query shape, you can use a [`$querySettings`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/querysettings.md#mongodb-pipeline-pipe.-querySettings) stage in an aggregation pipeline. The hash string is named `queryShapeHash` in the `$querySettings` output.

If you provide a query shape to `removeQuerySettings`, include the fields for the existing query settings shape to delete. The field values don't have to match. For example, if you have existing query settings for `find x=1` and provide `find x=100` to `removeQuerySettings`, `removeQuerySettings` deletes the query settings for `find x=1`.

For more information about query shapes, see [Query Shapes.](https://www.mongodb.com/docs/manual/core/query-shapes.md#std-label-query-shapes)

## 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

**Important:**

This command is not supported in M0 and Flex clusters. For more information, 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

You can delete query settings using either of the following syntax specifications.

### Provide a Query Shape Hash String

In the following syntax, you provide a query shape hash string in `removeQuerySettings`:

```javascript
db.adminCommand( {
   removeQuerySettings: <string>  // Provide an existing query shape hash string
} )
```

### Provide a Query Shape

In the following syntax, you provide:

- The same fields as a [`find`](https://www.mongodb.com/docs/manual/reference/command/find.md#mongodb-dbcommand-dbcmd.find), [`distinct`](https://www.mongodb.com/docs/manual/reference/command/distinct.md#mongodb-dbcommand-dbcmd.distinct), or [`aggregate`](https://www.mongodb.com/docs/manual/reference/command/aggregate.md#mongodb-dbcommand-dbcmd.aggregate) command. See the syntax sections on the pages for those commands for the fields you can include in `removeQuerySettings`.

- A `$db` field that specifies the database name associated with the original command.

```javascript
db.adminCommand( {
   removeQuerySettings: {
      <fields>,  // Provide fields for
                 // find, distinct, or aggregate command
      $db: <string>  // Provide a database name
   }
} )
```

## Command Fields

The command takes this field:

| Field | Type | Necessity | Description |
| --- | --- | --- | --- |
| `removeQuerySettings` | document or string | Required | You can provide either: The same fields as those in a [`find`](https://www.mongodb.com/docs/manual/reference/command/find.md#mongodb-dbcommand-dbcmd.find), [`distinct`](https://www.mongodb.com/docs/manual/reference/command/distinct.md#mongodb-dbcommand-dbcmd.distinct), or [`aggregate`](https://www.mongodb.com/docs/manual/reference/command/aggregate.md#mongodb-dbcommand-dbcmd.aggregate) command, and a `$db` field with the database associated with the original command.; An existing query shape hash string. |

## Examples

The following examples create a collection, add query settings, and delete the settings:

1. Create the example collection and index

   Run:

   ```javascript
   // Create pizzaOrders collection
   db.pizzaOrders.insertMany( [
      { _id: 0, type: "pepperoni", totalNumber: 5,
        orderDate: new Date( "2024-01-15T12:00:00Z" ) },
      { _id: 1, type: "cheese", totalNumber: 15,
        orderDate: new Date( "2024-01-23T11:12:32Z" ) },
      { _id: 2, type: "vegan", totalNumber: 20,
        orderDate: new Date( "2024-03-20T10:01:12Z" ) }
   ] )

   // Create ascending index on orderDate field
   db.pizzaOrders.createIndex( { orderDate: 1 } )
   ```

   The index has the default name `orderDate_1`.

2. Add the query settings

   The following `setQuerySettings` example adds query settings:

   ```javascript
   db.adminCommand( {
      setQuerySettings: {
         find: "pizzaOrders",
         filter: {
            orderDate: { $gt: ISODate( "2024-01-20T00:00:00Z" ) }
         },
         sort: {
            totalNumber: 1
         },
         $db: "test"
      },
      settings: {
         indexHints: {
            ns: { db: "test", coll: "pizzaOrders" },
            allowedIndexes: [ "orderDate_1" ]
         },
         queryFramework: "classic",
         comment: "Index hint for orderDate_1 index to improve query performance"
      }
   } )
   ```

3. Return the query settings

   The following example uses `$querySettings` to return the query settings:

   ```javascript
   db.aggregate( [
      { $querySettings: {} }
   ] )
   ```

   To locate the query settings to delete, use the `queryShapeHash` string in this output:

   ```javascript
   [
      {
         queryShapeHash: 'F42757F1AEB68B4C5A6DE6182B29B01947C829C926BCC01226BDA4DDE799766C',
         settings: {
            indexHints: {
               ns: { db: 'test', coll: 'pizzaOrders' },
               allowedIndexes: [ 'orderDate_1' ]
            },
            queryFramework: 'classic',
            comment: 'Index hint for orderDate_1 index to improve query performance'
         },
         representativeQuery: {
            find: 'pizzaOrders',
            filter: { orderDate: { '$gt': ISODate('2024-01-20T00:00:00.000Z') } },
            sort: { totalNumber: 1 },
            '$db': 'test'
         }
      }
   ]
   ```

4. Delete the query settings

   The following example uses `removeQuerySettings` to delete the query settings identified using `queryShapeHash` from the previous output:

   ```javascript
   db.adminCommand( {
      removeQuerySettings: "F42757F1AEB68B4C5A6DE6182B29B01947C829C926BCC01226BDA4DDE799766C"
   } )
   ```

   You can also delete query settings using a query shape. For example:

   ```javascript
   db.adminCommand( {
      removeQuerySettings: {
         find: "pizzaOrders",
         filter: {
            orderDate: { $gt: ISODate( "2023-01-20T00:00:00Z" ) }
         },
         sort: {
            totalNumber: 1
         },
         $db: "test"
      }
   } )
   ```

## Learn More

- [Query Plans](https://www.mongodb.com/docs/manual/core/query-plans.md#std-label-query-plans-query-optimization)

- [`setQuerySettings`](https://www.mongodb.com/docs/manual/reference/command/setquerysettings.md#mongodb-dbcommand-dbcmd.setQuerySettings)

- [`$querySettings`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/querysettings.md#mongodb-pipeline-pipe.-querySettings)

- [Query Shapes](https://www.mongodb.com/docs/manual/core/query-shapes.md#std-label-query-shapes)

- [Query statistics for query shapes](https://www.mongodb.com/docs/manual/reference/operator/aggregation/querystats.md#std-label-queryStats-queryShape)

- [Block Slow Queries with Operation Rejection Filters](https://www.mongodb.com/docs/manual/tutorial/operation-rejection-filters.md#std-label-operation-rejection-filters)
