How to delete Coupon data by Coupon id Magento 2?

Shopping cart Coupon deletes by coupon id in Magento 2 to get rid of all the expired coupons from the site.

Many of the coupons are created for specific events or some special days only like Black Friday or Cyber Monday after completed that day.

A client wants to delete all the expired ticket by coupon id, You can delete it safely with below code snippet.

Use deleteById() method to delete Coupon from the store.

<?php
namespace Jesadiya\DeleteCoupon\Model;

use Exception;
use Psr\Log\LoggerInterface;
use Magento\SalesRule\Api\CouponRepositoryInterface;

class CouponDelete
{
    /**
     * @var LoggerInterface
     */
    private $logger;

    /**
     * @var CouponRepositoryInterface
     */
    private $couponRepository;

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

    /**
     * Get Coupon Delete by id
     *
     * @return bool true on success
     */
    public function getCouponDeleteById()
    {
        $couponDelete = false;

        try {
            $couponId = 5; // coupon id
            $couponDelete = $this->couponRepository->deleteById($couponId);
        } catch (Exception $exception) {
            $this->logger->error($exception->getMessage());
        }

        return $couponDelete;
    }
}

if Coupon id found into the database table, delete it by id and gives result as true. Coupon Id is not found result will be false.

Output: Boolean value.