How to get Category Collection per Storewise in Magento 2?

When we have multi-store setup and store have different category assigned at that time we need to get store wise category collection.
We can get store wise category collection by below code snippet.

1. Use directly Objectmanager is not best/Recommended way to do in Magento, use Block with the construct() and fetch method in your phtml file.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$categoryFactory = $objectManager->create('Magento\Catalog\Helper\Category');
$categoryFactory->getStoreCategories(false,false,true);

2. Recommended Way Using Create Block file and call from the template,

Create Block PHP file,

<?php
namespace Rbj\Training\Block\Index;

class Training extends \Magento\Framework\View\Element\Template
{
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Catalog\Helper\Category $categoryHelper,
        array $data = []
    ) {
        $this->_categoryHelper = $categoryHelper;
        parent::__construct($context, $data);
    }

     /**
     * Retrieve current store level 2 category
     *
     * $sorted (if true display collection sorted as name otherwise sorted as based on id asc)
     * $asCollection (if true display all category otherwise display second level category menu visible category for current store)
     * $toLoad
     */
    public function getMainStoreCategories($sorted = false, $asCollection = false, $toLoad = true)
    {
        return $this->_categoryHelper->getStoreCategories($sorted , $asCollection, $toLoad);
    }

    /* Here we have passed sorted as true so category display as Name ASC order */
    public function getAllCurrentStoreCategories($sorted = true, $asCollection = true, $toLoad = true)
    {
        return $this->_categoryHelper->getStoreCategories($sorted , $asCollection, $toLoad);
    }
}

Display second level category, all those categories which have Level equals 2. call below function from a template file,

$getLevel2Category = $block->getMainStoreCategories();
foreach($getLevel2Category as $category) {
    echo $category->getName();echo "<br>";
}

For getting all categories of current store call below function in template file,

$getCategory = $block->getAllCurrentStoreCategories();
foreach($getCategory as $category) {
    echo $category->getName();echo "<br>";
}

You can change the custom argument by passing argument on above function from template file.

How to reindex indexer programmatically in magento 2?

Just create PHP file under your module’s Cron folder,
A created module like Rbj_Indexing for reference, Create Indexer.php file and call execute() function,

<?php
namespace Rbj\Indexing\Cron;

class Indexer
{
    public function __construct(
        \Magento\Indexer\Model\Processor $processor
    ) {
        $this->processor = $processor;
    }

    public function execute()
    {
        /* Regenerate indexes for all indexers */
        $this->processor->reindexAll();

        /* Regenerate indexes for all invalid indexers */
        $this->processor->reindexAllInvalid()
    }
}

There are two types of method for indexing.

1. Regenerate indexes for all indexers
$this->processor->reindexAll();

2. Regenerate indexes for all invalid indexers. This will only regenerate for invalid indexer.
$this->processor->reindexAllInvalid()

How to clean or flush cache programmatically in magento 2?

Many times we need to clear cache programmatically or using an external script, Using below code snippet we can clear of flush cache programmatically,

<?php
namespace Rbj\Cache\Block;

class CacheClear extends \Magento\Framework\View\Element\Template
{
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Framework\App\Cache\Frontend\Pool $cacheFrontendPool,
        \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
        array $data = []
    ) {
        $this->_cacheFrontendPool = $cacheFrontendPool;
        $this->_cacheTypeList = $cacheTypeList;
        parent::__construct($context, $data);
    }

     /* Using Product id */
    public function cacheClear()
    {
        /* get all types of cache in system */
        $allTypes = array_keys($this->_cacheTypeList->getTypes());

        /* Clean cached data for specific cache type */
        foreach ($allTypes as $type) {
            $this->_cacheTypeList->cleanType($type);
        }

        /* flushed the Entire cache storage from system, Works like Flush Cache Storage button click on System -> Cache Management */
        foreach ($this->_cacheFrontendPool as $cacheFrontend) {
            $cacheFrontend->getBackend()->clean();
        }
    }
}

Call function like below,
$productStockById = $block->cacheClear();

Get the list of all cache type by just below way,

$allTypes = array_keys($this->_cacheTypeList->getTypes());

Result like below type of all cache,

List of cache types:
array('config','layout','block_html','collections','reflection','db_ddl',
            'eav', 'customer_notification','config_integration','config_integration_api','full_page', 'translate', 'config_webservice');

If you want to flushAll cache, Use below methods,

foreach ($this->_cacheFrontendPool as $cacheFrontend) {
    $cacheFrontend->getBackend()->clean();
}