How to Drop the Table Using Declarative Schema in Magento 2.3?

Today, we’re going to teach you guys how to drop a table using the declarative schema in Magento 2.3.

With its recent update, they’ve introduced many new features including the declarative schema, which basically simplifies installing and updating the schema process.

In its earlier version, you had to create a script and perform an action to drop a table. But with Magento 2.3, they’ve simplified it by using the db_schema.xml file.

Also read: How to Create a New Table using Declarative Schema in Magento 2.3?

And in this tutorial, we’ll show you how to drop a table using declarative schema.

Steps to Drop a Table Using Declarative Schema in Magento 2.3

Step #1

First of all, you’ll have to create a table using the db_schema.xml file as shown below.

<?xml version="1.0"?>
<!--
/**
 * Created By : MageDelight Pvt. Ltd.
 */
-->
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
-    <table name="Md_helloworld" resource="default" engine="innodb" comment="Md Helloworld">
-        <column xsi:type="smallint" name="id" padding="6" unsigned="false" nullable="false" identity="true" comment="ID" />
-        <column xsi:type="varchar" name="author_name" nullable="false" length="25" comment="Name" />
-        <column xsi:type="varchar" name="email" nullable="false" length="25" comment="Email" />
-        <column xsi:type="varchar" name="description" nullable="false" length="255" comment="Descrition" />
-        <constraint xsi:type="primary" referenceId="PRIMARY">
-            <column name="id" />
-        </constraint>
-    </table>
</schema>

Also read: How to Add a New Column to an Existing Table Using db Schema in Magento 2?

Step #2

Once you’ve created the table using db_schema.xml file, you need to create a whitelist and upgrade your custom module by executing the following commands:

php bin/magento setup:db-declaration:generate-whitelist --module-name=Md_Helloworld
php bin/magento s:up

And it’s done!

Also read: How to Create a Custom Database Table in Magento 2?

Final Words…

This is how easy it is to drop a table using the db_schema.xml in Magento 2.3.

We hope you found this tutorial helpful. If you have any doubts, please share them in the comments below. And if you need our professional assistance, feel free to contact us anytime.