I used below code to delete a product from Magento Database programmatically and this code works for me.
$productEntityTable = Mage::getModel('importexport/import_proxy_product_resource')->getEntityTable();
if ($idToDelete) {
$this->db->query("DELETE FROM `{$productEntityTable}` WHERE `entity_id` IN (?)", $idToDelete);
echo 'Deleted';
}
But I want to delete product images also and for this, I used below piece of code
$_product = Mage::getModel('catalog/product')->load($idToDelete);
$mediaApi = Mage::getModel("catalog/product_attribute_media_api");
try {
$items = $mediaApi->items($_product->getId());
foreach($items as $item) {
echo ($mediaApi->remove($_product->getId(), $item['file']));
}
} catch (Exception $exception){
var_dump($exception);
die('Exception Thrown');
}
And I got following error and I Used many code but all the time I got the same error.
Fatal error: Call to a member function getUserId() on a non-object in /my_path/app/code/local/Mage/Catalog/Model/Product/Attribute/Backend/Media.php on line 263
May its asking for current user.
set current user as admin for this add below line top of the code