Drop a column from the table using db_schema.xml in Magento 2.

In Magento 2, You can create a new table using the db_schema.xml file. The table contains many columns based on your requirements.

Check the blog for, How to creates a new database table.

In the future, if you want to remove or drop some column from an existing table you can drop a column using db_schema.xml file.

Let’s assume, You have created a table name, my_custom_table in the database with the email column and you want to remove the email column.

Table with a list of entries and some of the columns you don’t require, You can remove those columns using the attribute disabled=true in a db_schema.xml file.

First of all, you must have a db_schema_whitelist.json file to remove columns from the table,

bin/magento setup:db-declaration:generate-whitelist --module-name=Vendor_Packagename

Using the above command, you can generate a db_schema_whitelist.json file inside your module etc directory with the above command.

Where Vendor_Packagename is your vendor and module name.

For example remove a column from a specific table, email column.

<column xsi:type="varchar" name="email" nullable="true" length="255" comment="Email"/>

Drop the email column using disabled=”true”,

<column xsi:type="varchar" name="email" nullable="true" length="255" comment="Email" disabled="true"/>

Where Vendorname_Packagename is your vendor and module name.

This file provides a history of all tables, columns, and keys added with declarative schema using a specific module.

Run the below command to drop a column
php bin/magento setup:upgrade

Now when seeing your table your column will be removed.