Magento 2.x. Update/add cms static block programmatically.

Magento 2.x. Update/add cms static block programmatically.

To update cms static block for particular store programmatically you can use following code.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$identifier = 'block_identifier';
$store_id = 2; 

try {
    $block = $objectManager->create('Magento\Cms\Model\Block');
    $block->setStoreId($store_id); // store for block you want to update 
    $block->load($identifier, 'identifier');
    $block->setIdentifier($identifier);
    $block->setTitle('Block Title');
    $block->setIsActive(1); 
    $block->setStores($store_id);
    $block->setContent($content);
    $block->save();

    echo "Static block updated! \n";
} catch (Exception $e) {
    echo $e->getMessage();
}