The question:
I created a table using the dbDelta function and then removed a few columns and added a few. The new columns did get created but the old columns did not get deleted from the table.
Is there a different way I am supposed to delete columns from the table ?
The Solutions:
Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.
Method 1
As far as I know, dbDelta()
is primarily used to add tables to the database. It can also add or alter columns. To delete columns, you should use $wpdb->query()
:
global $wpdb;
$table = $wpdb->prefix . 'table_name';
$wpdb->query( "ALTER TABLE $table DROP COLUMN column_name" );
Method 2
No, dbDelta can add or alter columns, but not drop them.
The code for the dbDelta function is in wp-admin/includes/upgrade.php. You can examine what it is capable of there.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0