Delete Shipment by shipment id Magento 2.

You can delete the Specific Shipment data by shipment id in Magento 2.

Using Magento\Sales\Api\ShipmentRepositoryInterface interface, you need to use delete() method to delete shipment data from the shipment table.

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

<?php
namespace Path\To\Class;

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

class ShipmentDeleteById
{
    /**
     * @var ShipmentRepositoryInterface
     */
    private $shipmentRepository;

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

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

    /**
     * Delete Shipment data by Shipment Id
     *
     * @param int $id
     *
     * @return bool
     */
    public function deleteShipment(int $id)
    {
        $deleteShipment = false;

        try {
            $shipment = $this->shipmentRepository->get($id);

            $deleteShipment = $this->shipmentRepository->delete($shipment);
        } catch (Exception $exception)  {
            $this->logger->critical($exception->getMessage());
        }
        return $deleteShipment;
    }
}

Output: Boolean