How to remove column from an existing table in Magento 2.3 using declarative schema? - Kishan Savaliya

Welcome to Kishan Savaliya's Magento 2 Blog.In this blog post, I will show you how to remove column from an existing table in Magento 2.3 using declarative schema. If you don't know how to create new table using Declarative Schema then this article can be useful to you!!

Before Magento 2.3, we can remove column from an existing table using UpgradeSchema.php file, but we can use declarative schema in Magento 2.3 or later versions to remove column(s) from an existing table.


It's very simple to remove column from an existing table with the use of declarative schema in Magento 2.3 or later versions, so let's start step-by-step..

I'm assuming you had created table using declarative schema in your custom module, so just go to you Magento 2 installation directory and navigate inside your custom module directory, inside your module directory navigate to etc/ directory and open db_schema.xml file.

app/code/SK/DeclarativeSchema/etc/db_schema.xml

Content for this file is..

<?xml version="1.0"?>
<!--
/**
 * SK DeclarativeSchema Magento 2.3 remove column from an existing table.
 */
-->
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
   <table name="sk_declaration_schema_new" resource="default" engine="innodb" comment="SK DeclarativeSchema">
      <column xsi:type="int" name="id" padding="10" unsigned="false" nullable="false" identity="true" comment="ID" />
      <column xsi:type="varchar" name="firstname" nullable="false" length="30" comment="Firstname" />
      <column xsi:type="varchar" name="lastname" nullable="false" length="30" comment="Lastname" disabled="true"/>
      <column xsi:type="varchar" name="email" nullable="false" length="45" comment="Email" />
      <column xsi:type="varchar" name="content" nullable="false" length="255" comment="Content" />
      <column xsi:type="varchar" name="mobile_number" nullable="false" length="20" comment="Mobile Number" />
      <constraint xsi:type="primary" referenceId="PRIMARY">
         <column name="id" />
      </constraint>
   </table>
</schema>

In above table, I want to remove lastname column from my existing table sk_declaration_schema_new, So I just added disabled="true" attribute in lastname column like this..

<column xsi:type="varchar" name="lastname" nullable="false" length="30" comment="Lastname" disabled="true"/>

That's it.. Now just upgrade your setup by running following commad in your terminal.

php bin/magento setup:upgrade

After running above command if you will check your database table column list in your MySQL, you can see lastname column is successfully removed from there.


Hope you may like this article and can understand this easily. You can add comments below in case if you have any questions regarding this article or if I missed anything here. I will check and get back to you with proper solution.

If you enjoyed this blog post, share it with friends!