You can use next plugin for your purpose:
<type name="Magento\Shipping\Model\Rate\Result">
<plugin name="your_plugin_name_update_rate_result"
type="Vendor\Module\Plugin\ShippingRateResultAppend"
sortOrder="10"
disabled="false" />
</type>
code will be like this one, but with own validation:
/**
* Disable the marked shipping rates.
*
* @param \Magento\Shipping\Model\Rate\Result $subject
* @param array $result
* @return array
*/
public function afterGetAllRates($subject, $result)
{
/** @var \Magento\Quote\Model\Quote $quote */
$quote = $this->getQuote(); // method must be defined in your class; you can get an actual quote from a session;
if (!$quote) {
// exception?
}
$removeAll = (int)$quote->getRemoveAll();
$customShippingMethodCode = 'custom_methodcode'; // static
/** @var \Magento\Quote\Model\Quote\Address\RateResult\Method[] $result */
/**
* @var int $key
* @var \Magento\Quote\Model\Quote\Address\RateResult\Method $rate
*/
foreach ($result as $key => $rate) {
$methodCode = $rate->getCarrier() . '_' . $rate->getMethod();
if ($removeAll && $methodCode !== $customShippingMethodCode) {
unset($result[$key]);
} elseif (!$removeAll && $methodCode === $customShippingMethodCode) {
unset($result[$key]);
}
}
return $result;
}