planCacheClear
Definition
planCacheClear
Removes cached query plans for a collection. Specify a plan cache query shape to remove cached query plans for that shape. Omit the plan cache query shape to clear all cached query plans.
Tip
In
mongosh
, this command can also be run through thePlanCache.clear()
andPlanCache.clearPlansByQuery()
helper methods.Helper methods are convenient for
mongosh
users, but they may not return the same level of information as database commands. In cases where the convenience is not needed or the additional return fields are required, use the database command.
Query Settings
Starting in MongoDB 8.0, use query settings instead of adding index filters. Index filters are deprecated starting in MongoDB 8.0.
Query settings have more functionality than index filters. Also, index
filters aren't persistent and you cannot easily create index filters for
all cluster nodes. To add query settings and explore examples, see
setQuerySettings
.
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 is not supported in M0, M2, and M5 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 syntax:
db.runCommand( { planCacheClear: <collection>, query: <query>, sort: <sort>, projection: <projection>, comment: <any> } )
Command Fields
The command takes the following optional fields:
Field | Type | Description |
---|---|---|
| document | Optional. The query predicate of the plan cache query shape. Only the structure of the predicate, including the field names, are significant to the shape; the values in the query predicate are insignificant. |
| document | Optional. The projection associated with the plan cache query shape. |
| document | Optional. The sort associated with the plan cache query shape. |
| 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). |
To see the query shapes for which cached query plans exist, see Examples.
Required Access
On systems running with authorization
, a user must have access that
includes the planCacheWrite
action.
Examples
Clear Cached Plans for a Plan Cache Query Shape
If a collection orders
has the following plan cache query shape:
{ "query" : { "qty" : { "$gt" : 10 } }, "sort" : { "ord_date" : 1 }, "projection" : { }, "planCacheShapeHash" : "9AAD95BE" }
Warning
Starting in MongoDB 8.0, the pre-existing queryHash
field is renamed
to planCacheShapeHash
. If you're using an earlier MongoDB version,
you'll see queryHash
instead of planCacheShapeHash
.
The following operation clears the query plan cached for the shape:
db.runCommand( { planCacheClear: "orders", query: { "qty" : { "$gt" : 10 } }, sort: { "ord_date" : 1 } } )
Clear All Cached Plans for a Collection
The following example clears all the cached query plans for the
orders
collection:
db.runCommand( { planCacheClear: "orders" } )