Delete Invoice by invoice id Magento 2.

You can delete invoice data by invoice id in Magento 2.

Using Magento\Sales\Api\InvoiceRepositoryInterface interface you need to use get() and delete() function to delete invoice by id.

You must fetch the invoice object by id and then pass the invoice object as a parameter in delete() method to delete the specific invoice.

<?php
namespace Path\To\Class;

use Exception;
use Psr\Log\LoggerInterface;
use Magento\Sales\Api\InvoiceRepositoryInterface;

class Demo
{
    /**
     * @var InvoiceRepositoryInterface
     */
    private $invoiceRepository;

    /**
     * @var LoggerInterface
     */
    private $logger;

    public function __construct(
        InvoiceRepositoryInterface $invoiceRepository,
        LoggerInterface $logger
    ) {
        $this->invoiceRepository = $invoiceRepository;
        $this->logger = $logger;
    }

    /**
     * Delete Invoice by invoice id
     *
     * @return bool
     */
    public function deleteInvoice(): bool
    {
        $invoiceId = 1;
        $deleteInvoice = false;
        try {
            $invoiceData = $this->invoiceRepository->get($invoiceId);
            //delete invoice by invoice object
            $deleteInvoice = $this->invoiceRepository->delete($invoiceData);
        } catch (Exception $exception)  {
            $this->logger->critical($exception->getMessage());
        }

        return $deleteInvoice;
    }
}

Using the above way you can delete the invoice using Invoice Repository.

Get Invoice data by invoice id Magento 2.

You can get full details of Invoice related data using invoice id in Magento 2.

Using Magento\Sales\Api\InvoiceRepositoryInterface class you can get details of invoice related data in Magento 2.

Get Invoice related data using below code snippet in Magento 2 by Invoice id,

Write a Delete SQL query statement in Magento 2.

Write a MySQL delete query using Magento’s standard way of deleting specific rows from the database table by Magento.

You can write direct SQL query delete() without worrying about Model operation using a given code snippet in the blog.