36

How do you drop an index only if it exists?

It seems simple but I did found anything on the net. The idea is to drop it only if it exists, because if not, I will have an error and my process stops.

I found this to find if the index exists:

select index_name
from user_indexes
where table_name = 'myTable'
and index_name='myIndexName'

But I don't know how to put it together with

DROP INDEX myIndexName
4
  • 3
    @Samuel's is the most correct of the solutions. IMHO, that's the one that should have been accepted.
    – Vadim K.
    Apr 27, 2010 at 16:14
  • Beware of DDL commands' implicit commits!! That drop commits, like or not. Wrap it into an autonomous transaction if necessary.
    – Samuel
    Apr 27, 2010 at 16:22
  • 1
    The reason why there is no straightforward solution is because the situation should not arise. This is a configuration management issue.
    – APC
    Apr 27, 2010 at 16:26
  • 2
    @APC That is the difference between an ideal world and ours. Situations come up where you have to do things that "shouldn't have to be done". Sometimes, it's because someone made a bad decision before. Sometimes, it's because the situation changes and suddenly makes a choice that was good at the time a bad one. "It shouldn't happen" is never a valid reason for leaving out a feature, in my opinion. It can be a valid reason for allocating resources on more important problems, but several other database technologies provide something like DROP IF EXISTS, and I appreciate when it's there.
    – jpmc26
    Nov 16, 2013 at 3:31

5 Answers 5

75

Don't check for existence. Try to drop, and capture the exception if necessary...

DECLARE
   index_not_exists EXCEPTION;
   PRAGMA EXCEPTION_INIT (index_not_exists, -1418);
BEGIN
   EXECUTE IMMEDIATE 'drop index foo';
EXCEPTION
   WHEN index_not_exists
   THEN
      NULL;
END;
/
4
  • 2
    What advantage over the accepted answer does this have, aside from the fact you could probably replace COUNT with EXISTS?
    – jpmc26
    Nov 16, 2013 at 3:35
  • 6
    @jpmc26 Accepted answer has a race condition. The index could have been dropped between the line that checks the count and the execute immediate.
    – Samuel
    Dec 16, 2013 at 9:00
  • 1
    @Samuel While that's a valid point, I would hope that people would not have 2 independent commands trying to perform DDL on the same object at the same time. ;)
    – jpmc26
    Jan 20, 2014 at 20:12
  • 2
    Welcome to the world of multi-threading. :( Great answer +1
    – Richard
    Apr 24, 2014 at 12:55
33
DECLARE
   COUNT_INDEXES   INTEGER;
BEGIN
   SELECT COUNT ( * )
     INTO COUNT_INDEXES
     FROM USER_INDEXES
    WHERE INDEX_NAME = 'myIndexName';
   -- Edited by UltraCommit, October 1st, 2019
   -- Accepted answer has a race condition.
   -- The index could have been dropped between the line that checks the count
   -- and the execute immediate
   IF COUNT_INDEXES > 0
   THEN
      EXECUTE IMMEDIATE 'DROP INDEX myIndexName';
   END IF;
END;
/
3
  • 3
    Of course, "DROP INDEX" already checks for the existence of the index, so you're doing it twice now. Apr 28, 2010 at 4:21
  • 1
    This technique introduces a race condition. Please consider using the answer that @Samuel provided instead.
    – JoeNahmias
    Sep 23, 2019 at 13:55
  • I have alerted users that my accepted answer has a race condition. I suggest using the answer that @Samuel provided instead. I cannot delete my answer, because it has been accepted in 2010 Oct 1, 2019 at 12:58
3

In Oracle, you can't mix both DDL and DML. In order to do so, you need to work it around with the EXECUTE IMMEDIATE statement.

So, first check for the existence of the index.

Second, drop the index through the EXECUTE IMMEDIATE statement.

DECLARE v_Exists NUMBER;

BEGIN
    v_Exists := 0;

    SELECT 1 INTO v_Exists
        FROM USER_INDEXES
        WHERE TABLE_NAME LIKE 'myTable'
            AND INDEX_NAME LIKE 'myIndexName'

    IF v_Exists = 1 THEN
        EXECUTE IMMEDIATE "DROP INDEX myIndexName"
    ENDIF;

    EXCEPTION
        WHEN OTHERS THEN
            NULL;
END;

This code is out the top of my head and you may need to fix it up a little, but this gives an idea.

Hope this helps! =)

1
  • The first statement is enough, brilliant explanation. Thanks!
    – dimplex
    Dec 15, 2022 at 20:55
0

I made a procedure so it can be called several times:

DELIMITER €€
DROP PROCEDURE IF EXISTS ClearIndex€€
CREATE PROCEDURE ClearIndex(IN var_index VARCHAR(255),IN var_table VARCHAR(255))
BEGIN
    SET @temp = concat('DROP INDEX ', var_index, ' ON ', var_table);
    PREPARE stm1 FROM @temp;
    BEGIN
        DECLARE CONTINUE HANDLER FOR 1091 SELECT concat('Index ', var_index,' did not exist in ',var_table,', but was handled') AS 'INFO';
            EXECUTE stm1;
    END;
END €€
DELIMITER ;

Now it can be called more than once:

CALL ClearIndex('employees_no_index','employees');
CALL ClearIndex('salaries_no_index','salaries');
CALL ClearIndex('titles_no_index','titles');
1
  • 1
    Instead of DROP PROCEDURE you can use CREATE OR REPLACE PROCEDURE
    – psur
    Aug 22, 2013 at 9:48
-1

I hope this will help. It's a combination of all solution :) By the way thanks for the help !

CREATE OR REPLACE PROCEDURE CLEAR_INDEX(INDEX_NAME IN VARCHAR2) AS
BEGIN
    EXECUTE IMMEDIATE 'drop index ' || INDEX_NAME;
EXCEPTION
    WHEN OTHERS THEN
        NULL;
END CLEAR_INDEX;
1
  • 1
    You're ignoring ALL exceptions here, definitely not a good practice.
    – JoeNahmias
    Sep 23, 2019 at 13:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.