Drop Indexes

  • how-to
    +
    How to drop primary and secondary indexes.

    Introduction

    You can drop primary and secondary indexes when you do not need them any more. Dropping an index that has replicas will also drop all of the replica indexes too.

    If you want to try out the examples in this section, follow the instructions given in Do a Quick Install to install Couchbase Server, configure a cluster, and load a sample dataset. Read the following for further information about the tools available for editing and executing queries:

    Dropping a Primary Index

    You can drop a primary index using a SQL++ statement or an SDK call.

    The SDK calls only enable you to drop indexes in the default collection and default scope within a bucket. A SQL++ statement enables you to drop indexes in any collection and scope within a bucket.
    • SQL++

    • .NET

    • Java

    • Node.js

    • Python

    To drop an unnamed primary index, use the DROP PRIMARY INDEX statement.

    To drop a named primary index, use the DROP INDEX statement. There are two possible syntaxes:

    • Specify the index name, then use the ON keyword to specify the keyspace which contains the index.

    • Specify the keyspace and index name using dotted notation.


    Context

    For this example, set the query context to the inventory scope in the travel sample dataset. For more information, see Setting the Query Context.

    Queries

    The following query drops an unnamed primary index from the airline keyspace.

    DROP PRIMARY INDEX ON airline;

    The following query drops a named primary index from the airline keyspace.

    DROP INDEX travel_primary ON airline;

    The following query drops the index in exactly the same way, but uses alternative syntax.

    DROP INDEX airline.travel_primary;

    For further details and examples, refer to DROP PRIMARY INDEX and DROP INDEX.

    To drop a primary index, use the task DropPrimaryIndexAsync() on the interface IQueryIndexManager.

    1. Specify the keyspace which contains the index.

    2. If the index has a name:

      1. Use DropPrimaryQueryIndexOptions to specify the index options.

      2. In the index options, invoke the IndexName method.


    The following example drops an unnamed primary index from the specified keyspace.

    await cluster.QueryIndexes.DropPrimaryIndexAsync("`travel-sample`");

    The following example drops a named primary index from the specified keyspace.

    await cluster.QueryIndexes.DropPrimaryIndexAsync(
    	"`travel-sample`",
    	options => options.IndexName("named_primary_index")
    );

    Click the View button to see this code in context.

    For further details, refer to IQueryIndexManager().

    To drop a primary index, use the dropPrimaryIndex method and specify the keyspace which contains the index.

    The Java SDK does not provide a call for dropping a named primary index. To drop a named primary index, use a SQL++ query.

    The following example drops an unnamed primary index from the specified keyspace.

    cluster.queryIndexes().dropPrimaryIndex("travel-sample");

    Click the View button to see this code in context.

    For further details, refer to QueryIndexManager.

    To drop a primary index, use the dropPrimaryIndex function on a QueryIndexManager object.

    1. Specify the keyspace which contains the index.

    2. If the index has a name:

      1. Use DropPrimaryQueryIndexOptions to specify the index options.

      2. In the index options, use the name property to specify the index name.


    The following example drops an unnamed primary index from the specified keyspace.

    await cluster.queryIndexes().dropPrimaryIndex('travel-sample')

    The following example drops a named primary index from the specified keyspace.

    await cluster
      .queryIndexes()
      .dropPrimaryIndex('travel-sample', { name: 'named_primary_index' })

    Click the View button to see this code in context.

    For further details, refer to QueryIndexManager.

    To drop a primary index, use the drop_primary_index function on a QueryIndexManager object.

    1. Specify the keyspace which contains the index.

    2. If the index has a name:

      1. Use DropPrimaryQueryIndexOptions to specify the index options.

      2. In the index options, use the index_name property to specify the index name.


    The following example drops an unnamed primary index from the specified keyspace.

    cluster.query_indexes().drop_primary_index(
        "travel-sample"
    )

    The following example drops a named primary index from the specified keyspace.

    cluster.query_indexes().drop_primary_index(
        "travel-sample",
        DropPrimaryQueryIndexOptions(index_name="named_primary_index")
    )

    Click the View button to see this code in context.

    For further details, refer to SQL++ Index Management.

    Dropping a Secondary Index

    You can drop a secondary index using a SQL++ statement or an SDK call.

    The SDK calls only enable you to drop indexes in the default collection and default scope within a bucket. A SQL++ statement enables you to drop indexes in any collection and scope within a bucket.
    • SQL++

    • .NET

    • Java

    • Node.js

    • Python

    To drop a a secondary index, use the DROP INDEX statement. There are two possible syntaxes:

    • Specify the index name, then use the ON keyword to specify the keyspace which contains the index.

    • Specify the keyspace and index name using dotted notation.


    Context

    For this example, set the query context to the inventory scope in the travel sample dataset. For more information, see Setting the Query Context.

    Queries

    The following query drops a named index from the airline keyspace.

    DROP INDEX `idx-name` ON airline;

    The following query drops the index in exactly the same way, but uses alternative syntax.

    DROP INDEX airline.`idx-name`;

    For further details and examples, refer to DROP INDEX.

    To drop a secondary index, use the task DropIndexAsync() on the interface IQueryIndexManager.

    1. Specify the keyspace which contains the index.

    2. Specify the name of the index.


    The following example drops a named index from the specified keyspace.

    await cluster.QueryIndexes.DropIndexAsync("`travel-sample`", "index_name");

    Click the View button to see this code in context.

    For further details, refer to IQueryIndexManager().

    To drop a secondary index, use the dropIndex method.

    1. Specify the keyspace which contains the index.

    2. Specify the name of the index.


    The following example drops a named index from the specified keyspace.

    cluster.queryIndexes().dropIndex("travel-sample", "index_name");

    Click the View button to see this code in context.

    For further details, refer to QueryIndexManager.

    To drop a secondary index, use the dropIndex function on a QueryIndexManager object.

    1. Specify the keyspace which contains the index.

    2. Specify the name of the index.


    The following example drops a named index from the specified keyspace.

    await cluster.queryIndexes().dropIndex('travel-sample', 'index_name')

    Click the View button to see this code in context.

    For further details, refer to QueryIndexManager.

    To drop a secondary index, use the drop_index function on a QueryIndexManager object.

    1. Specify the keyspace which contains the index.

    2. Specify the name of the index.


    The following example drops a named index from the specified keyspace.

    cluster.query_indexes().drop_index("travel-sample", "index_name")

    Click the View button to see this code in context.

    For further details, refer to SQL++ Index Management.

    Reference and explanation:

    Administrator guides:

    Indexes with SDKs: