How to rename an existing custom table in Magento 2?

Welcome to Kishan Savaliya's Magento 2 Blog.In previous blog post, we learned how to create custom table in Magento 2 using Install and Upgrade scripts. Hope you like that tutorial and you had created your custom table successfully.

Now, we will learn how to rename an existing table name in Magento 2. In Previous blog, we had created sk_custom_table table, we will try to rename that table name with custom_table_sk. You can follow below steps to rename an existing table in Magento 2.

Go to your Magento 2 root directory, and navigate inside app/code/VendorName/ModuleName. In previous blog you can find steps to create new module and how to register and enable new module.

We have created SK as Vendor name and CustomTable as module name.


Open module.xml file and upgrade version inside that file.

<module name="SK_CustomTable" setup_version="1.1.2">

Now, navigate to below directory.


Content for this file is..

* SK Create custom table Magento 2 with the use of
* InstallSchema, InstallData, UpgradeSchema and UpgradeData

namespace SK\CustomTable\Setup;

use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

class UpgradeSchema implements UpgradeSchemaInterface
    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
        if (version_compare($context->getVersion(), '1.1.2', '<')) {
            $setup->getConnection()->renameTable($setup->getTable('sk_custom_table'), $setup->getTable('custom_table_sk'));

Now, open your terminal and run below command on your Magento 2 root path.

php bin/magento setup:upgrade

That's it. Now you can see your new table name in your database. Now you can verify your new table name using below command.

echo "use magento2; SHOW TABLES LIKE '%custom_table%'" | mysql -uroot -p

Here in above command I have used my database name magento2, you can put your database name there. I am trying to find custom_table that is my custom table name, you can put your table name there, and you can use your MySQL username and password in above command where I used root as my username of MySQL.

How to rename an existing custom table in magento 2?

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!