I am trying to create/update static block programmatically with recurringData.php from setup script.

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Customer\Model\GroupFactory;
use Magento\Cms\Model\BlockFactory;
class RecurringData implements InstallDataInterface

protected $storeManager;    
protected $_objectManager;
private  $blockFactory;
protected $_urlInterface;
protected $state;   

public function __construct(

    \Magento\Store\Model\StoreManagerInterface $storeManager,
    \Magento\Framework\ObjectManagerInterface $objectManager,       
    BlockFactory $blockFactory,
    \Magento\Framework\UrlInterface $urlInterface,
    \Magento\Framework\App\State $state
) {
    $this->storeManager = $storeManager;
    $this->_objectManager = $objectManager;
    $this->blockFactory = $blockFactory;
    $this->_urlInterface    = $urlInterface;
    $this->state = $state;

  public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
  $storeId = $this->storeManager->getStore()->getId();
   $customBlock= $this->blockFactory->create()->setStoreId($storeId)->load('test-block', 'identifier');
      $objectManager  = $this->_objectManager;   
       $media_dir = $objectManager->get('Magento\Store\Model\StoreManagerInterface')
                $Image = $media_dir."test.png";

                $contentBlock = [ 
                    'title' => 'Test Block',
                    'identifier' => 'test-block',
                    'stores' => $storeId,
                    'is_active' => 1,
                    'content' => '<div class="test-update">
                                    <h3>Static Block</h3>
                                    <div class="update-options">
                                    <div class="test-section"><a href="#"><img src="'.$Image .'" alt="">Testing</a>
                                    <p>checking data</p>
                    'sort_order' => 0
                if (!$customBlock->getId()) {
                    $this->blockFactory->create()->setData($contentBlock )->save();
                } else {
                    $customBlock->setContent($contentBlock ['content'])->save();

Here I have already block with same identifier and i am trying to update the content of it, I am getting below error.

A block identifier with the same properties already exists in the selected store.

Am i used the correct code? Can anyone help me to implement it. Thanks


3 Answers 3


It seems like there are duplicate cms block available with different store. This error occurred from this file :


 * Perform operations before object save
 * @param AbstractModel $object
 * @return $this
 * @throws LocalizedException
protected function _beforeSave(AbstractModel $object)
    if (!$this->getIsUniqueBlockToStores($object)) {
        throw new LocalizedException(
            __('A block identifier with the same properties already exists in the selected store.')
    return $this;

Debug in this function and check identifier and block id from this $object.

  • 1
    Yes there was a duplicate cms block associated with different store,@Rohan Nov 7, 2019 at 7:36

Set Block Id when you update the content.

if (!$customBlock->getId()) {

     $this->blockFactory->create()->setData($contentBlock )->save();
} else {
     $customBlock->setId($customBlock->getId())->setContent($contentBlock ['content'])->save();

Hope it will fix your issue.

  • or just use addData instead of setData to get rid of if statement Jan 30, 2023 at 16:21

Try out this module https://github.com/markshust/magento2-module-simpledata which simplifies the process a lot. It provides a simple API to create/modify/delete static blocks, pages.

Creating a block becomes then:

declare(strict_types = 1);

namespace MarkShust\Data\Setup\Patch\Data;

use MarkShust\SimpleData\Setup\Patch\SimpleDataPatch;

class BlockFooBarCreate extends SimpleDataPatch
    public function apply()
            'identifier' => 'foo_bar',
            'title' => 'Foo bar',
            'content' => <<<CONTENT
<div class="foo-bar">
    Foo bar

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.