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 4242032
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:23:17+00:00 2026-05-21T03:23:17+00:00

After large redesigning of DB I need to remove all indices from it, and

  • 0

After large redesigning of DB I need to remove all indices from it, and set new indices.
I found good scrpipt to get all indices and remove but I have problem – I cannot remove indices created by PRIMARY KEY constraint. (maybe there are also other kind of indices that I cannot remove).

My question is: how to change code below to remove all indices except indices created for primary keys or other that I cannot remove manually.

DECLARE @indexName VARCHAR(128)
DECLARE @tableName VARCHAR(128)

DECLARE [indexes] CURSOR FOR
    SELECT  [sysindexes].[name] AS [Index], [sysobjects].[name] AS [Table]
    FROM [sysindexes]
    INNER JOIN [sysobjects] ON [sysindexes].[id] = [sysobjects].[id]    
    WHERE [sysindexes].[name] IS NOT NULL AND [sysobjects].[type] = 'U'

OPEN [indexes]

FETCH NEXT FROM [indexes] INTO @indexName, @tableName
WHILE @@FETCH_STATUS = 0
BEGIN
    EXEC ('DROP INDEX [' + @indexName + '] ON [' + @tableName + ']')
    FETCH NEXT FROM [indexes] INTO @indexName, @tableName
END

CLOSE [indexes]
DEALLOCATE [indexes]
  • 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-05-21T03:23:18+00:00Added an answer on May 21, 2026 at 3:23 am

    Based on Marc_s answer I found correct version of script:

    SELECT 'DROP INDEX ' + name + ' ON ' + object_name(object_id)
    FROM sys.indexes
    WHERE is_primary_key = 0 AND is_unique_constraint = 0
    AND OBJECTPROPERTY(object_id, 'IsMSShipped') = 0     -- exclude system-level tables
    AND name IS NOT NULL
    

    Checking if object_Id is greater than 255 isn’t sufficient:
    – we had to use OBJECTPROPERTY(object_id, ‘IsMSShipped’) = 0, because some system tables like queue_messages have very high id
    – also we have to check if index is created by unique constraint : is_unique_constraint = 0

    After those improvements it’ll remove all indices created by user.

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

Sidebar

Related Questions

After transferring a large image from a REST endpoint, I need to divide the
I have a large PHP application. After I changed some settings I get a
After profiling a large game playing program, I have found that the library function
After switching from 64-bit to 32-bit platform (both of them are CentOS) I get
I am trying to write text file after reading from a large tab delimited
I need to allocate very large arrays of simple structs (1 GB RAM). After
Say I instantiate a large object with var and new . After I'm done
After redesigning an app we got larger load times on new views create (the
I'm collecting a large number of tweets via tweepy, but after a while, it
I have a large image which after labeling has about 500 features. I know

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.