How do I assign a region to programmatically created block? Hi after following this guide I was able to create multiple blocks.
But I have couple of issues.


Blocks are not created instantly need to clear cache but cant find a way to do it instantly (solution might be clearing block cache but how ?).
I need to assign region to the block while creating itself but cant do so .
I want to have x content for x block id and y content for y block id cant write a filter condition as I cant get block id of the blocks.


Basically I want to achieve all the D7 module's functionality in D8.

If anyone have an idea please help me.
That's expected. You can do it from code if needed. The following are the steps to follow.

Settings definition is done as in block.block.test_block.yml. Place that file, after editing it, in my_module/config/install and you're set. A quick hack is managing the block settings manually in a local environment and export them from /admin/config/development/configuration/single/export.

Import your settings, which can be done using code similar to the following one.
use Drupal\Core\Config\FileStorage;

$config_path = drupal_get_path('module', 'my_module') . '/config/install';
$source = new FileStorage($config_path);
$config_storage = \Drupal::service('config.storage');
$block_config = 'block.block.test_block';
$config_storage-&gt;write($block_config , $source-&gt;read($block_config));

Block settings are theme dependent.


For dynamic allocation of blocks, you can load and alter the block entity like this.
use Drupal\block\Entity\Block;

$block = Block::load('test_block');
$block-&gt;setRegion('my_region');
$block-&gt;save();