How To

How to write a Delete SQL query using Standard way without Model File in Magento 2

Howdy Devs,
For any CMS, interacting with MYSQL to fetch and retrieve data from the database is like a dependable bond where one needs another to work flawlessly. If you see the Magento 2 which is one of the most used Ecommerce CMS, it also uses MYSQL where it stores different store information like order data, customer details, etc. Many times it happens that being a developer you need to perform some database operations whether it is storing, restriving or deleting records from the database while coding.
According to Magento coding standards, one must create a Model file to perform SQL operations but what if you can perform SQL query without using model file by following Magento coding standards? Yes! When there is a will, there is a way too. Here is I have performed delete SQL query to show you a demo that how you can delete a particular number of rows/records without using a model file in Magento 2.
To do this I have created on ‘Index.php’ file inside my extension folder at this path using below code.
app\code\Vendor\Extension\Controller\Deletequery\

<pre class="lang:default decode:true">
<?php
namespace Vendor\Extension\Controller\Deletequery;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\App\Action\Context;
class Index extends \Magento\Framework\App\Action\Action
{
 const QUOTE_TABLE = 'quote';
 private $resourceConnection;
 public function __construct(
      Context $context,
      ResourceConnection $resourceConnection)
 {
     $this->resourceConnection = $resourceConnection;
     return parent::__construct($context);
 }
 public function execute()
 {
     $connection  = $this->resourceConnection->getConnection();
     $tableName = $connection->getTableName(self::QUOTE_TABLE);
     $quoteId = 191;
     $whereConditions = [
            $connection->quoteInto('entity_id = ?', $quoteId),
     ];
     $deleteRows = $connection->delete($tableName, $whereConditions);
     echo "Deleted Rows : ".$deleteRows;
 }
}
</pre>

That’s it. Using this code you can perform similar different SQL operations without creating standard Magento 2 Model file.

Lastly, if you found this blog helpful, don’t forget to share it with your colleagues and Magento Friends and Let us know if you are facing any issue while implementing this code.

Happy Coding!

Click to rate this post!
[Total: 9 Average: 4.4]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

1 day ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

3 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

3 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

4 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

5 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

1 week ago