Magento, Magento 2

Magento 2.3 : Drop Table using Declarative Schema

Magento 2.3 Drop Table using Declarative Schema

In this tutorial, we will learn about how to drop the table using declarative schema in Magento 2.3. In Magento 2.3, There are introduce new features about declarative schema which major role is going to simplify installing and updating the schema process. So, here we will learn about how to drop the table using declarative schema.

In the future, whenever you want to drop a table you can simply perform an action using db_schema.xml file. In the previous version, there is no way about drop the table as Magento way. We need to create script for that and perform action to delete the table. But, In Magento 2.3 you can drop the table by using the db_schema.xml file.

Let’s assume that you have created a table using db_schema.xml from here :

<?xml version="1.0"?>
<!--
/**
 * Created By : Rohan Hapani
 */
-->
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
-    <table name="rh_helloworld" resource="default" engine="innodb" comment="RH 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>

You just need to remove that line from db_schmea.xml file. After that, you need to again generate whitelist and upgrade your custom module.

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

I hope this blog will helpful for easily understand how to drop the table using declarative schema in Magento 2.3.x. In case, I missed anything or need to add some information, always feel free to leave a comment in this blog, I’ll get back with proper solution 🙂

Tagged ,