Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9255553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:44:29+00:00 2026-06-18T11:44:29+00:00

How to delete all indexes from one database, whether clustered or nonclustered? I need

  • 0

How to delete all indexes from one database, whether clustered or nonclustered?

I need do that with script, not over GUI.

EDITED

Database has 7 tables, some of them are lookups, some are related over foreign keys. Every table has minimal one index, created in time the primary key was created, so automatically was created constraint. When deleting such indexes over an GUI, I got an error that indexes cannot be deleted because of dependency on other keys.

So, I need to first delete an indexes keys that are foreign keys, and then an indexes created over primary keys.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T11:44:30+00:00Added an answer on June 18, 2026 at 11:44 am

    Slightly different variation of dynamic SQL, however it drops foreign keys first, then primary keys, then indexes (non-clustered indexes first so that you don’t convert to a heap (which can affect all the non-clustered indexes)).

    USE specific_database;
    GO
    

    First, delete all foreign keys:

    DECLARE @sql NVARCHAR(MAX);
    
    SET @sql = N'';
    
    SELECT @sql = @sql + N'ALTER TABLE ' 
      + QUOTENAME(OBJECT_SCHEMA_NAME([parent_object_id]))
      + '.' + QUOTENAME(OBJECT_NAME([parent_object_id])) 
      + ' DROP CONSTRAINT ' + QUOTENAME(name) + ';
    '
    FROM sys.foreign_keys
    WHERE OBJECTPROPERTY([parent_object_id], 'IsMsShipped') = 0;
    
    EXEC sp_executesql @sql;
    

    Now drop primary keys:

    DECLARE @sql NVARCHAR(MAX);
    
    SET @sql = N'';
    
    SELECT @sql = @sql + N'ALTER TABLE '
      + QUOTENAME(OBJECT_SCHEMA_NAME([parent_object_id]))
      + '.' + QUOTENAME(OBJECT_NAME([parent_object_id])) 
      + ' DROP CONSTRAINT ' + QUOTENAME(name) + ';
    '
    FROM sys.key_constraints 
    WHERE [type] = 'PK'
    AND OBJECTPROPERTY([parent_object_id], 'IsMsShipped') = 0;
    
    EXEC sp_executesql @sql;
    

    And finally, indexes, non-clustered first:

    DECLARE @sql NVARCHAR(MAX);
    
    SET @sql = N'';
    
    SELECT @sql = @sql + N'DROP INDEX ' 
      + QUOTENAME(name) + ' ON '
      + QUOTENAME(OBJECT_SCHEMA_NAME([object_id]))
      + '.' + QUOTENAME(OBJECT_NAME([object_id])) + ';
    '
    FROM sys.indexes 
    WHERE index_id > 0
    AND OBJECTPROPERTY([object_id], 'IsMsShipped') = 0
    ORDER BY [object_id], index_id DESC;
    
    EXEC sp_executesql @sql;
    

    Note that the database engine tuning advisor will recommend a bunch of these indexes (and depending on the workload you present it, may miss some, and may suggest redundant and nearly duplicate indexes). However it is not going to recommend any of the data integrity stuff you just deleted (PK, FK, unique constraints).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a script to delete all tables in my database that looks like
I need to delete all the folders in current directory that starts with say
Is it possible to delete all databases that were created by a specific user?
How to delete all changes from working directory including new untracked files. I know
I would like to delete all the rows found by that query: SELECT cart_abandon.*
DELETE FROM keywords WHERE NOT EXISTS (SELECT keywords_relations.k_id FROM keywords_relations WHERE keywords.k_id = keywords_relations.k_id)
I have a substantial database... not a very large one - around 1gb of
Is there a MySQL command that can drop all the extra indexes except for
I want to delete all files in a directory with a given name, except
I want to delete all rows in a datatable. I use something like this:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.