diff --git a/lib/internal/Magento/Framework/Code/GeneratedFiles.php b/lib/internal/Magento/Framework/Code/GeneratedFiles.php
index a550a740bcfd7c33cc11634910498cc2cc596a31..972990c4377ba2005589443baa5cf096257d72b5 100644
--- a/lib/internal/Magento/Framework/Code/GeneratedFiles.php
+++ b/lib/internal/Magento/Framework/Code/GeneratedFiles.php
@@ -5,9 +5,12 @@
  */
 namespace Magento\Framework\Code;
 
+use Magento\Framework\Config\Data\ConfigData;
 use Magento\Framework\App\Filesystem\DirectoryList;
+use Magento\Framework\Config\File\ConfigFilePool;
 use Magento\Framework\Filesystem\Directory\WriteFactory;
 use Magento\Framework\Filesystem\Directory\WriteInterface;
+use Magento\Framework\App\DeploymentConfig\Writer\PhpFormatter;
 
 /**
  * Regenerates generated code and DI configuration
@@ -49,6 +52,43 @@ class GeneratedFiles
     public function regenerate()
     {
         if ($this->write->isExist(self::REGENERATE_FLAG)) {
+            //clean cache
+            $deploymentConfig = $this->directoryList->getPath(DirectoryList::CONFIG);
+            $configPool = new ConfigFilePool();
+            $envPath = $deploymentConfig . '/' . $configPool->getPath(ConfigFilePool::APP_ENV);
+            if ($this->write->isExist($this->write->getRelativePath($envPath))) {
+                $cacheData = include $envPath;
+
+                if (isset($cacheData['cache_types'])) {
+                    $enabledCacheTypes = $cacheData['cache_types'];
+                    $enabledCacheTypes = array_filter($enabledCacheTypes, function ($value) {
+                            return $value;
+                        }
+                    );
+                    if (!empty($enabledCacheTypes)) {
+                        $this->write->writeFile($this->write->getRelativePath(
+                                $this->directoryList->getPath(DirectoryList::VAR_DIR)) . '/.cachestates.json',
+                            json_encode($enabledCacheTypes)
+                        );
+                        $cacheTypes = array_keys($cacheData['cache_types']);
+
+                        foreach ($cacheTypes as $cacheType) {
+                            $cacheData['cache_types'][$cacheType] = 0;
+                        }
+
+                        $formatter = new PhpFormatter();
+                        $contents = $formatter->format($cacheData);
+
+                        $this->write->writeFile($this->write->getRelativePath($envPath), $contents);
+                        if (function_exists('opcache_invalidate')) {
+                            opcache_invalidate(
+                                $this->write->getAbsolutePath($envPath)
+                            );
+                        }
+                    }
+                }
+            }
+            $cachePath = $this->write->getRelativePath($this->directoryList->getPath(DirectoryList::CACHE));
             $generationPath = $this->write->getRelativePath($this->directoryList->getPath(DirectoryList::GENERATION));
             $diPath = $this->write->getRelativePath($this->directoryList->getPath(DirectoryList::DI));
 
@@ -58,6 +98,11 @@ class GeneratedFiles
             if ($this->write->isDirectory($diPath)) {
                 $this->write->delete($diPath);
             }
+            if ($this->write->isDirectory($cachePath)) {
+                $this->write->delete($cachePath);
+            }
+            //add to queue
+
             $this->write->delete(self::REGENERATE_FLAG);
         }
     }
diff --git a/setup/src/Magento/Setup/Model/Cron/JobUpgrade.php b/setup/src/Magento/Setup/Model/Cron/JobUpgrade.php
index ef2e12606bd041eead0147fc79b79874b39cf485..c5367611f3b064ec054e6171db14d506b38cd9cb 100644
--- a/setup/src/Magento/Setup/Model/Cron/JobUpgrade.php
+++ b/setup/src/Magento/Setup/Model/Cron/JobUpgrade.php
@@ -5,6 +5,8 @@
  */
 namespace Magento\Setup\Model\Cron;
 
+use Magento\Framework\App\Filesystem\DirectoryList;
+use Magento\Framework\Filesystem;
 use Magento\Setup\Console\Command\AbstractSetupCommand;
 use Magento\Setup\Model\ObjectManagerProvider;
 use Symfony\Component\Console\Input\ArrayInput;
@@ -70,6 +72,26 @@ class JobUpgrade extends AbstractJob
             );
             $this->params['command'] = 'setup:upgrade';
             $this->command->run(new ArrayInput($this->params), $this->output);
+
+            /**
+             * @var \Magento\Framework\Filesystem\Directory\WriteFactory $writeFactory
+             */
+            $writeFactory = $this->objectManager->get('\\Magento\Framework\Filesystem\Directory\WriteFactory');
+            $write = $writeFactory->create(BP);
+            $dirList = $this->objectManager->get('\Magento\Framework\App\Filesystem\DirectoryList');
+            $pathToCacheStatus = $write->getRelativePath(
+                $dirList->getPath(DirectoryList::VAR_DIR) . '/.cachestates.json'
+            );
+
+            if ($write->isExist($pathToCacheStatus)) {
+                $params = array_keys(json_decode($write->readFile($pathToCacheStatus), true));
+
+                $this->queue->addJobs(
+                    [['name' => JobFactory::JOB_ENABLE_CACHE, 'params' =>  [implode(' ', $params)]]]
+                );
+                $write->delete($pathToCacheStatus);
+            }
+
         } catch (\Exception $e) {
             $this->status->toggleUpdateError(true);
             throw new \RuntimeException(sprintf('Could not complete %s successfully: %s', $this, $e->getMessage()));