Azure Cosmos DB CLI

Manage your Cosmos DB resources from the command line

Getting Started

The Azure CLI (az) provides a powerful set of commands to manage Azure Cosmos DB resources. Ensure you have the Azure CLI installed and logged in to your Azure account.

Installation

Follow the official Azure CLI installation guide for your operating system:

Install the Azure CLI

Login

Log in to your Azure account:

az login

Set Subscription (if needed)

az account set --subscription ""

Cosmos DB Extension

The Cosmos DB commands are part of the cosmosdb extension. Install or update it:

az extension add --name cosmosdb
az extension update --name cosmosdb

Account Management

Create an Azure Cosmos DB account

This command creates a new Azure Cosmos DB account. You'll need to specify a resource group, account name, location, and API type.

az cosmosdb create --name --resource-group --kind --locations region= [--table-name ]

Example for SQL API:

az cosmosdb create --name mycosmosdbaccount --resource-group myresourcegroup --locations regionName=eastus

List Azure Cosmos DB accounts

az cosmosdb list [--resource-group ]

Show account details

az cosmosdb show --name --resource-group

Delete an Azure Cosmos DB account

az cosmosdb delete --name --resource-group

Database Management

Create a database

az cosmosdb sql database create --account-name --resource-group --name [--throughput ]

Example:

az cosmosdb sql database create --account-name mycosmosdbaccount --resource-group myresourcegroup --name myDatabase --throughput 400

List databases

az cosmosdb sql database list --account-name --resource-group

Show database details

az cosmosdb sql database show --account-name --resource-group --name

Delete a database

az cosmosdb sql database delete --account-name --resource-group --name --yes

Container Management

Create a container

az cosmosdb sql container create --account-name --resource-group --database-name --name --partition-key-path [--throughput ]

Example:

az cosmosdb sql container create --account-name mycosmosdbaccount --resource-group myresourcegroup --database-name myDatabase --name myContainer --partition-key-path "/city" --throughput 1000

List containers

az cosmosdb sql container list --account-name --resource-group --database-name

Show container details

az cosmosdb sql container show --account-name --resource-group --database-name --name

Delete a container

az cosmosdb sql container delete --account-name --resource-group --database-name --name --yes

Item Management (SQL API)

Create an item

Items are created by passing JSON content.

az cosmosdb sql item create --account-name --resource-group --database-name --container-name --document <'{ "id": "item1", "name": "sample", "city": "Tokyo" }'>

List items

You can use a SQL query to list items.

az cosmosdb sql item list --account-name --resource-group --database-name --container-name --query "SELECT * FROM c"

Show an item

az cosmosdb sql item show --account-name --resource-group --database-name --container-name --item-id --partition-key

Update an item

az cosmosdb sql item update --account-name --resource-group --database-name --container-name --item-id --partition-key --document <'{ "id": "item1", "name": "updated sample", "city": "London" }'>

Delete an item

az cosmosdb sql item delete --account-name --resource-group --database-name --container-name --item-id --partition-key --yes

Throughput Management

Update throughput for a database

az cosmosdb sql database update --account-name --resource-group --name --throughput

Update throughput for a container

az cosmosdb sql container update --account-name --resource-group --database-name --name --throughput

Autoscale Settings

You can also configure autoscale settings for databases and containers:

az cosmosdb sql database update --account-name --resource-group --name --max-throughput
az cosmosdb sql container update --account-name --resource-group --database-name --name --max-throughput

Region Management

Add a region to an account

az cosmosdb create --name --resource-group --locations regionName= --failover-priority

List regions for an account

az cosmosdb show --name --resource-group --query locations

Failover to a region

This command promotes a region to be the primary for write operations.

az cosmosdb failover-priority-group failover --account-name --resource-group --failover-priority-group-id --locations

Backup & Restore

Continuous Backup (Point-in-Time Restore)

Continuous backup is enabled by default on new accounts with the SQL API. You can configure the retention period.

az cosmosdb update --name --resource-group --continuous-backup-enabled true --continuous-backup-retention-period

Restore an account

Restore to a specific point in time. This requires continuous backup to be enabled.

az cosmosdb restore --account-name --resource-group --target-name --restore-timestamp
Tip: Use the --query parameter with most commands to filter and format the output effectively. For example, az cosmosdb list --query "[?kind=='GlobalDocumentDB']".
Warning: Deleting an Azure Cosmos DB account or database is irreversible. Always double-check your commands before execution.