Drop Index, Recreate and Script out Indexes in SQL Server

By:   |   Updated: 2024-03-28   |   Comments   |   Related: > Indexing


Indexes are very helpful to improve SQL Server query performance, but sometimes indexes get created that are not helpful or no longer needed. In this article, we look at various ways to delete a SQL Server index.


The most common reasons to drop an index are because it's either unused or a duplicate of another index. While indexes can (and usually do) improve performance, unused or duplicate indexes take up storage space unnecessarily. They can slow things down, which is the opposite of what we, as DBAs and Developers, are trying to accomplish.

Key Details About Disabling and Dropping SQL Server Indexes

Let's look at some "must-know" information about disabling and dropping indexes.

  1. Be aware if you disable a clustered index
    • Prevents access to the data.
    • The data remains intact but inaccessible until the clustered index is dropped or rebuilt.
  2. An index may be part of a foreign key constraint.
    • Understanding the relationships and constraints involved with the index is imperative.
  3. An index may still be in use.
    • Be sure you're tracking index data for a reasonable period of time. Remember, unless you're capturing Dynamic Management View (DMV) data, they only hold data going back to the last server restart.
    • Create and store an index creation script for any index you plan to drop somewhere safe, such as in source control, just in case. Recreating an index from a known good script is much easier than restoring an old backup to a test server.
  4. An application, stored procedure, view, or function may be using an index hint(s).
    • The definitions for stored procedures, views, or functions are easily searched, but you may not have access to every app's source code, especially if it's a vendor application.
  5. You may violate a vendor support contract.
    • Some application vendors take the position that there are no changes of any kind to be made in their databases.
    • The methods pointed out can still be used to make a case for dropping them to present to the vendor for their approval.
  6. Disabled indexes are not maintained.
  7. A disabled index prohibits creating another index with the same name.
  8. The optimizer does not consider disabled indexes.
  9. A disabled index can be dropped.
  10. Disabling an index associated with primary keys, foreign keys, or constraints disables them on the other table(s).
  11. Foreign key constraints on underlying tables are also disabled when a clustered index is disabled.
  12. Non-clustered indexes are disabled when an associated clustered index is disabled.
  13. Statistics cannot be successfully updated on a table with a disabled clustered index.

Finding Unused SQL Server Indexes

The following tips show ways to help find unused indexes:

Finding Duplicate SQL Server Indexes

These tips will show you ways to find duplicate indexes:

Disabling SQL Server Indexes

It's safer to disable an index than to drop it. As cautious DBAs, we may want to consider disabling what we believe is an unused or duplicate index as a first step. Re-enabling an index is quicker and easier than recreating them.

The minimum permission to disable an index is ALTER permission on the table or view. You already have permission if you are in the sysadmin server role or db_ddladmin or db_owner roles in the database.

For our examples, we'll work with a non-clustered index, IX_ExpectedDeliveryDate, on the Purchasing.PurchaseOrders table in the WideWorldImporters sample database.

To disable with SQL Server Management Studio (SSMS), expand down to the index in the Object Explorer

  1. Right-click on the index to disable.
  2. Click Disable.
Disable Index 1

You're presented with the Disable Indexes confirmation window.

  1. Click OK.
Disable Index 2

Or you can disable the index with SQL:

USE [WideWorldImporters]
ALTER INDEX [IX_ExpectedDeliveryDate] ON [Purchasing].[PurchaseOrders] DISABLE;

Enabling a Disabled SQL Server Index

If it's determined late that the index is needed, we must rebuild it to re-enable.

  1. Right-click on the index.
  2. Click Rebuild.
Rebuild Index 1

You're presented with the Rebuild Indexes confirmation window.

  1. Click OK.
Rebuild Index 2

Or rebuild to re-enable the index with T-SQL:

USE [WideWorldImporters]
ALTER INDEX [IX_ExpectedDeliveryDate] ON [Purchasing].[PurchaseOrders]

Dropping SQL Server Indexes

The minimum permission needed to drop an index is also ALTER permission on the table or view.

Script Index Before Dropping

Where we can't simply rebuild an index as we did with a disabled index as the index no longer exists. It's highly advisable to archive a script to recreate it, if need be, before dropping it. You just never know.

  1. Right-click on the index.
  2. Click on Script Index as.
  3. Click CREATE To.
  4. Click on File…
Script Index Creation 1
  1. Choose a directory to write a file to.
  2. Name the script.
  3. Click Save.
Script Index Creation 2

Now, drop the index.

  1. Right-click on the index.
  2. Click Delete.
Drop Index 1

You're presented with the Delete Object confirmation window.

  1. Click OK.
Drop Index 2

Or drop the index with T-SQL:

USE [WideWorldImporters];
DROP INDEX [IX_ExpectedDeliveryDate] ON [Purchasing].[PurchaseOrders];

Recreate the Index if needed

To recreate the index, open the script you saved and run it.

Recreate Index
Next Steps

Here are additional links to index related tips:

sql server categories

sql server webinars

subscribe to mssqltips

sql server tutorials

sql server white papers

next tip

About the author
MSSQLTips author Joe Gavin Joe Gavin is from the Greater Boston area. He started working with SQL Server and Sybase in 1998 in the financial services industry and has been a SQL Server Database Administrator for a dairy cooperative since 2011. He graduated from Northeastern University in Boston with a Bachelor of Science in Engineering Technology (BSET) degree in Computer Technology. Joe has spoken at the Boston and Providence SQL Saturday events.

This author pledges the content of this article is based on professional experience and not AI generated.

View all my tips

Article Last Updated: 2024-03-28

Comments For This Article

get free sql tips
agree to terms