Title: add_clean_index
Published: April 25, 2014
Last modified: May 20, 2026

---

# add_clean_index( string $table, string $index ): true

## In this article

 * [Parameters](https://developer.wordpress.org/reference/functions/add_clean_index/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/add_clean_index/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/add_clean_index/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/add_clean_index/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/add_clean_index/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/add_clean_index/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/functions/add_clean_index/?output_format=md#wp--skip-link--target)

Adds an index to a specified table.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/add_clean_index/?output_format=md#parameters)󠁿

 `$table`stringrequired

Database table name.

`$index`stringrequired

Database table index column.

## 󠀁[Return](https://developer.wordpress.org/reference/functions/add_clean_index/?output_format=md#return)󠁿

 true True, when done with execution.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/add_clean_index/?output_format=md#source)󠁿

    ```php
    function add_clean_index( $table, $index ) {
    	global $wpdb;

    	drop_index( $table, $index );
    	$wpdb->query( "ALTER TABLE `$table` ADD INDEX ( `$index` )" );

    	return true;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-admin/includes/upgrade.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.0/src/wp-admin/includes/upgrade.php#L2743)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.0/src/wp-admin/includes/upgrade.php#L2743-L2750)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/add_clean_index/?output_format=md#related)󠁿

| Uses | Description | 
| [drop_index()](https://developer.wordpress.org/reference/functions/drop_index/)`wp-admin/includes/upgrade.php` |

Drops a specified index from a table.

  | 
| [wpdb::query()](https://developer.wordpress.org/reference/classes/wpdb/query/)`wp-includes/class-wpdb.php` |

Performs a database query, using current database connection.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/add_clean_index/?output_format=md#changelog)󠁿

| Version | Description | 
| [1.0.1](https://developer.wordpress.org/reference/since/1.0.1/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/add_clean_index/?output_format=md#user-contributed-notes)󠁿

 1.   [Skip to note 3 content](https://developer.wordpress.org/reference/functions/add_clean_index/?output_format=md#comment-content-3902)
 2.    [MakeWebBetter](https://profiles.wordpress.org/makewebbetter/)  [  6 years ago  ](https://developer.wordpress.org/reference/functions/add_clean_index/#comment-3902)
 3.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_clean_index%2F%23comment-3902)
     Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_clean_index%2F%23comment-3902)
 4.  Sometimes after a table has been created in a database, we find that it is advantageous
     to add an index to that table to speed up queries involving this table.
 5.      ```php
         function wpdocs_mwb_make_fetch_fast()
         {
             global $wpdb;
             // Add some Clean up indices
             add_clean_index( $wpdb->posts, 'post_name' );
             add_clean_index( $wpdb->categories, 'category_nicename' );
             add_clean_index( $wpdb->comments, 'comment_approved' );
             add_clean_index( $wpdb->posts, 'post_status' );
         }
         ```
     
 6.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_clean_index%2F%3Freplytocom%3D3902%23feedback-editor-3902)
 7.   [Skip to note 4 content](https://developer.wordpress.org/reference/functions/add_clean_index/?output_format=md#comment-content-7255)
 8.    [nimlab](https://profiles.wordpress.org/nimlab/)  [  1 year ago  ](https://developer.wordpress.org/reference/functions/add_clean_index/#comment-7255)
 9.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_clean_index%2F%23comment-7255)
     Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_clean_index%2F%23comment-7255)
 10. Adding indexes to existing tables can significantly enhance query performance,
     especially in large databases. The **add_clean_index** function simplifies this
     process by automatically removing an existing index before creating a new one,
     preventing potential conflicts and duplication.
 11. Recommendations:
 12.  a. **Check Existing Indexes:** Before adding an index, ensure that it is necessary
         and does not already exist in the table. This helps avoid redundant indexes that
         can slow down insert and update operations.
      b. **Index Naming:** Use meaningful names for indexes to facilitate their identification
         and management in the future. For example, instead of a simple post_name, use**
         idx_posts_post_name**.
      c. **Error Handling:** Consider extending the function to handle potential errors
         during SQL query execution. This will help identify and resolve issues during
         the development phase.
 13.     ```php
         function add_clean_index( $table, $index ) {
             global $wpdb;
     
             if ( drop_index( $table, $index ) ) {
                 $result = $wpdb->query( $wpdb->prepare( “ALTER TABLE `%s` ADD INDEX (`%s`)”, $table, $index ) );
                 if ( false === $result ) {
                     // Log the error or notify the administrator
                     error_log( “Failed to add index `$index` to table `$table`.” );
                     return false;
                 }
                 return true;
             }
             return false;
         }
         ```
     
 14.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_clean_index%2F%3Freplytocom%3D7255%23feedback-editor-7255)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_clean_index%2F)
before being able to contribute a note or feedback.