Magento – How to delete object from collection in magento2

collection;deletemagento2object

How to remove any object from magento 2 collection.
If we have collection of products and we want to delete product which is at first index . how to delete that product without deleting it from database?

$collection->addAttributeToSelect('*')->addFieldToFilter('attribute',$somevalue);

foreach ($collection as $key => &$value) {
              if($key == 1){
                //// how to unset this index in collection ?
              }

            }

Best Answer

This works perfectly

  $productCollection->removeItemByKey($product->getEntityId());
Related Topic