How to Exclude a Property from Indexing in Azure Cosmos DB with the Azure CLI
Azure Cosmos DB indexes every property of every item you write, unless you tell it otherwise. When no query ever filters or sorts on a property, such as an article's long body text, an indexing policy can leave it out of the index. Here's how to write that policy as a JSON file and apply it with the Azure CLI, to a new container and to one that already exists.
Every container has an indexing policy, a small JSON document that says which properties of its items go into the index. Microsoft's page on indexing policies describes the default: The default indexing policy for newly created containers indexes every property of every item and enforces range indexes for any string or number. That's why a query can filter on any property without any setup. It also means that every write updates the index for every property, including the ones no query ever looks at. The same page: By optimizing the number of paths that are indexed, you can substantially reduce the latency and RU charge of write operations.
The policy lists included paths and excluded paths. A path names a property much like a file path names a file: /body is the body property at the top level of an item, and what comes after it says how much of the property the path covers.
Syntax
The policy file:
{
"indexingMode": "consistent",
"includedPaths": [
{ "path": "/*" }
],
"excludedPaths": [
{ "path": "<path>" }
]
}
The command that creates a container with it:
- bash / zsh
- PowerShell
az cosmosdb sql container create \
--account-name <account-name> \
--resource-group <resource-group> \
--database-name <database-name> \
--name <container-name> \
--partition-key-path <partition-key-path> \
--idx @<policy-file>
az cosmosdb sql container create `
--account-name <account-name> `
--resource-group <resource-group> `
--database-name <database-name> `
--name <container-name> `
--partition-key-path <partition-key-path> `
--idx '@<policy-file>'
For a container that already exists, az cosmosdb sql container update takes the same parts, without --partition-key-path.
| Part | What it is |
|---|---|
"indexingMode": "consistent" | The index changes with every write: The index is updated synchronously as you create, update, or delete items. The other mode, none, turns indexing off |
"includedPaths" | The paths to index. /* is the root of the item, so it means every property |
"excludedPaths" | The paths to leave out of the index, one { "path": "<path>" } object each |
<path> | /<property>/? for the value of a property that holds a string or a number, /<property>/* for a property and everything below it, and /<property>/[]/<child>/? for a property of every element of an array |
<account-name> | The Azure Cosmos DB for NoSQL account |
<resource-group> | The resource group that holds the account |
<database-name> | The database that holds the container |
<container-name> | The container |
<partition-key-path> | The container's partition key path, such as /id. Only create takes it |
@<policy-file> | The policy file. The Azure CLI reference describes --idx as Indexing Policy, you can enter it as a string or as a file, and an @ before a name means a file. In PowerShell the value goes in quotes |
The paths follow three rules from Microsoft's page. The first: a path leading to a scalar value (string or number) ends with /?. The elements of an array are addressed all together with /[], instead of one by one as /0, /1 and so on. And a path that ends in /* covers everything below that property.
Every policy must have the root /* in one of the two lists.
Putting the root in includedPaths and listing only what to leave out is the approach Microsoft recommends: This approach is recommended as it lets Azure Cosmos DB proactively index any new property that might be added to your model.
The Example
A small blog keeps its articles in the container articles of the database howto, with the partition key path /id. Each article has a long body text and a list of comments, and no query ever filters or sorts on either of them, so both can leave the index. I created the container with a policy that leaves out body, changed the policy to leave out comments too, and read it back. The commands use my account, cosmos-maf-servicedesk-4224, and its resource group, rg-maf-servicedesk: use your own.
Prerequisites
- The Azure CLI, signed in with
az login. I used version 2.93.0. - An Azure role on the Cosmos DB account that can manage its containers, such as Owner or Contributor. Creating and changing a container is management, so a Cosmos DB data role doesn't do it.
Step 1: Write the Policy File
Save this as articles-indexing.json in the folder where you'll run the Azure CLI:
{
"indexingMode": "consistent",
"includedPaths": [
{ "path": "/*" }
],
"excludedPaths": [
{ "path": "/body/?" }
]
}
"includedPaths": [{ "path": "/*" }]indexes every property, as the default policy does, so a property the app adds later is indexed too."excludedPaths": [{ "path": "/body/?" }]leaves out the value ofbody./?at the end of a path means the property's own value, a string or a number, andbodyis a string."indexingMode": "consistent"keeps the index up to date with every write, as the default does.
Step 2: Create the Container with the Policy
- bash / zsh
- PowerShell
az cosmosdb sql container create \
--account-name cosmos-maf-servicedesk-4224 \
--resource-group rg-maf-servicedesk \
--database-name howto \
--name articles \
--partition-key-path /id \
--idx @articles-indexing.json \
--query "{container:name, indexingPolicy:resource.indexingPolicy}"
az cosmosdb sql container create `
--account-name cosmos-maf-servicedesk-4224 `
--resource-group rg-maf-servicedesk `
--database-name howto `
--name articles `
--partition-key-path /id `
--idx '@articles-indexing.json' `
--query "{container:name, indexingPolicy:resource.indexingPolicy}"
--partition-key-path /idmakes every article its own partition, found by its ID.--idx @articles-indexing.jsongives the container the policy in the file. In PowerShell,@starts a splatting expression, so the value is in quotes: Microsoft's page on running the Azure CLI in PowerShell says you can enclose the value in single (') or double (") quotes.--querykeeps two things from the long output: the container's name and its indexing policy.
It took 35 seconds:
{
"container": "articles",
"indexingPolicy": {
"automatic": true,
"compositeIndexes": null,
"excludedPaths": [
{
"path": "/body/?"
},
{
"path": "/\"_etag\"/?"
}
],
"fullTextIndexes": [],
"includedPaths": [
{
"indexes": null,
"path": "/*"
}
],
"indexingMode": "consistent",
"spatialIndexes": null,
"vectorIndexes": null
}
}

The excluded paths hold /body/? from the file and one more that the file doesn't have: /\"_etag\"/?. In JSON, \" is a double quote inside a string, so the path is /"_etag"/?, the system property _etag with its name in double quotes. Microsoft's page: The system property _etag is excluded from indexing by default, unless the etag is added to the included path for indexing. The quotes don't change what the path means: Functionally, it doesn't make any difference if you escape every path or just the ones that have special characters.
The other properties of the policy are kinds of index that this one doesn't use, composite, full-text, spatial and vector, so they're empty. automatic is true, as in the Azure CLI's default policy. The item's ID and its last-modified time aren't in the policy, but they're indexed anyway. Microsoft's page on managing indexing policies: The system properties id and _ts are always indexed when the cosmos account indexing mode is consistent.
Step 3: Leave Out a Whole Property on an Existing Container
No query filters on comments either. Change articles-indexing.json so it leaves out comments too:
{
"indexingMode": "consistent",
"includedPaths": [
{ "path": "/*" }
],
"excludedPaths": [
{ "path": "/body/?" },
{ "path": "/comments/*" }
]
}

/comments/*leaves outcommentsand everything below it: every element of the list, and every property inside the elements. Microsoft's page describes the/*wildcard as one that can match any elements below the node./body/?stays in the file.--idxsets the container's whole indexing policy to what the file says, so the file needs every path, the old one and the new one.
Then update the container. az cosmosdb sql container update takes the same --idx; this time --query keeps only the excluded paths:
- bash / zsh
- PowerShell
az cosmosdb sql container update \
--account-name cosmos-maf-servicedesk-4224 \
--resource-group rg-maf-servicedesk \
--database-name howto \
--name articles \
--idx @articles-indexing.json \
--query "{container:name, excludedPaths:resource.indexingPolicy.excludedPaths}"
az cosmosdb sql container update `
--account-name cosmos-maf-servicedesk-4224 `
--resource-group rg-maf-servicedesk `
--database-name howto `
--name articles `
--idx '@articles-indexing.json' `
--query "{container:name, excludedPaths:resource.indexingPolicy.excludedPaths}"
It took 37 seconds:
{
"container": "articles",
"excludedPaths": [
{
"path": "/body/?"
},
{
"path": "/comments/*"
},
{
"path": "/\"_etag\"/?"
}
]
}

Both paths from the file are there, and _etag again. Taking a path out of the index works at once, and putting one back in doesn't, as Microsoft's page explains: Removing an index takes effect immediately, whereas adding a new index takes some time as it requires an indexing transformation. That transformation runs online and asynchronously, and Index transformation is an operation that consumes request units. Either way, the app keeps working while the policy changes: When you update indexing policy, writes to Azure Cosmos DB are uninterrupted.
Step 4: Read the Policy Back
az cosmosdb sql container show prints a container's settings, and --query "resource.indexingPolicy" keeps only its indexing policy:
- bash / zsh
- PowerShell
az cosmosdb sql container show \
--account-name cosmos-maf-servicedesk-4224 \
--resource-group rg-maf-servicedesk \
--database-name howto \
--name articles \
--query "resource.indexingPolicy"
az cosmosdb sql container show `
--account-name cosmos-maf-servicedesk-4224 `
--resource-group rg-maf-servicedesk `
--database-name howto `
--name articles `
--query "resource.indexingPolicy"
{
"automatic": true,
"compositeIndexes": null,
"excludedPaths": [
{
"path": "/body/?"
},
{
"path": "/comments/*"
},
{
"path": "/\"_etag\"/?"
}
],
"fullTextIndexes": [],
"includedPaths": [
{
"indexes": null,
"path": "/*"
}
],
"indexingMode": "consistent",
"spatialIndexes": null,
"vectorIndexes": null
}

That's the policy the container now has: everything is indexed except body, comments and _etag. I also ran Step 3's update and this command in PowerShell 7.6, with '@articles-indexing.json' in quotes, and both printed the same output as in bash.
What a Query on an Excluded Property Costs
A property that's out of the index can still be used in a query, but the query can't use an index for it. Microsoft's page on indexing policies, in its section on changing a policy: When you drop an indexed path, the query engine will immediately stop using it, and will do a full scan instead.
I didn't run queries against articles. Microsoft's guide to troubleshooting query issues has numbers for this case, with a container whose policy excludes /description/* and this query:
SELECT *
FROM c
WHERE c.description = "Malabar spinach, cooked"
With description excluded, the query cost 409.51 RUs. With the excluded path removed, so that description was indexed again, the same query cost 2.98 RUs. The guide's rule: Your indexing policy should cover any properties included in WHERE clauses, ORDER BY clauses, JOIN, and most system functions. So leave out only the properties that no query filters, sorts or joins on.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Passing @articles-indexing.json without quotes in PowerShell | PowerShell takes the @ for splatting, and Microsoft's page lists that form as one that will not work in PowerShell | Quote it: '@articles-indexing.json' |
| Putting only the new path in the file for an update | --idx sets the whole indexing policy to the file, so a path missing from the file is no longer excluded | Keep every path in the file, the old ones and the new one |
| Excluding a property that a query filters or sorts on | The query can't use an index for it and scans instead, as in Microsoft's 409.51 RU example | Exclude only properties that no query uses |
Setting "indexingMode": "none" to save request units | Nothing is indexed, and it isn't possible to activate TTL on a container where the indexing mode is set to none | Keep consistent and exclude the paths you don't need |
FAQ
Are the Item's ID and Timestamp Indexed Too?
Yes, always, and they don't appear in the policy. Microsoft's page on managing indexing policies: The system properties id and _ts aren't included in the container policy's indexed paths description. This is by design because these system properties are indexed by default and this behavior can't be disabled.
What Policy Does a New Container Get by Default?
The Azure CLI reference gives --idx this default value:
{ "indexingMode": "consistent", "automatic": true, "includedPaths": [ { "path": "/*" } ], "excludedPaths": [ { "path": "/\"_etag\"/?" } ] }
Every property is indexed except _etag, the same policy as Step 2's output without /body/?.
Does Changing the Policy Take the Container Offline?
No. Microsoft's page: The old indexing policy is efficiently transformed to the new policy without affecting the write availability, read availability, or the throughput provisioned on the container. If you have several changes to make, the same page recommends making them in one update: If multiple indexing policy updates have to be made, it's recommended to do all the changes as a single operation in order to have the index transformation complete as quickly as possible.
Can I Change the Policy in the Azure Portal?
Yes. Microsoft's page on managing indexing policies: Azure Cosmos DB containers store their indexing policy as a JSON document that the Azure portal lets you directly edit. It's the same JSON as the file in this post.
Conclusion
To leave a property out of the index in Azure Cosmos DB, write the container's indexing policy as a JSON file that includes the root path and lists what to leave out in excludedPaths: /<property>/? for a single value, or /<property>/* for a property with everything below it. Pass the file to az cosmosdb sql container create or update with --idx @<file>, in quotes in PowerShell, and read the result back with az cosmosdb sql container show. Cosmos DB adds the _etag path itself, and the update replaces the whole policy, so keep every path in the file. Leave out only what no query filters, sorts or joins on.
Additional Resources
- Indexing policies in Azure Cosmos DB — indexing modes, property paths, the include and exclude strategy, and what happens when a policy changes.
- Manage indexing policies in Azure Cosmos DB — example policies and the ways to update one.
- az cosmosdb sql container — the
create,updateandshowcommands and the--idxparameter. - Considerations for running the Azure CLI in a PowerShell scripting language — why an
@value needs quotes in PowerShell. - Troubleshoot query issues when using Azure Cosmos DB — the example of a query on an excluded path, and how to read query metrics.
