Magento 2 clear cache programmatically

In this post, let’s find out how to clear/flush cache programmatically in Magento 2. It will be useful for you in your custom module or new feature. 

Whenever you make any modifications to your Magento website, you will need to flush cache to see the changes. And as you may know, there are some ways to do that.

  1. Go to System -> Tools -> Cache Management then select Flush Magento Cache
  2. Remove var/cache directory using File Manager in cPanel, FTP or SSH
  3. Using code ( helpful for automation ) 


In Magento 1


In Magento 2

  • Define constructor

  • Create a function to flush magento cache

You can edit $types variable if you don’t want to clear all cache types.


Using above code, we can clear Magento cache instead of using magento admin panel or removing var/cache folder. If you need any assitance, leave us a message

Magento 2 clear cache programmatically
5 (100%) 1 vote

Written By Metagento

Leave a Reply

Your email address will not be published.