How to write delete sql query in magento 2 ?

How to write delete sql query in magento 2 ?
  • To delete rows of table using where condition show the below code.
  • You can use this code in any file.
  • You can delete multiple tables row using this code.

use Magento\Framework\App\ResourceConnection;

class DeleteQuery {

const ORDER_TABLE = 'sales_order';

private $resourceConnection;

public function __construct(

ResourceConnection $resourceConnection

) {

$this->resourceConnection = $resourceConnection;

}

public function deleteQuery()

{

$connection = $this->resourceConnection->getConnection();

$tableName = $connection->getTableName(self::ORDER_TABLE);

$orderId = 41;

$whereConditions = [

$connection->quoteInto('order_id = ?', $orderId),

];

$deleteRows = $connection->delete($tableName, $whereConditions);

return $deleteRows;

}

}



Copyright © 2024 Tridhya Tech Limited, Inc. All rights reserved.