How to drop 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 drop 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 drop table using UpgradeSchema.php file, but we can use declarative schema in Magento 2.3 or later versions to drop table.


Very easy step is here in declarative schema to drop table.

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" disabled="true" />
</schema>

Just remove all the columns line from <table> tags and add disabled="true" attribute in <table> tag.

<table name="sk_declaration_schema_new" resource="default" engine="innodb" comment="SK DeclarativeSchema" disabled="true" />

Now, you just need to upgrade your setup by running following command.

php bin/magento setup:upgrade

Then go to your database and check, your table will not be there after running setup upgrade command.


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!