Today we will discuss the topic of How to Disable the Cron Job in Magento 2 as many times you are in a situation where you have to disable the cron job of some third-party modules.
In Magento 2, cron jobs do not have to disable features like observer and plugin. So let’s check out how we can do that.
How to Disable the Cron Job in Magento 2
To disable a cron job using the config in Magento 2, you can use the following steps:
- Create a new custom module in your Magento 2 installation.
- Add a crontab.xml file in your custom module in a folder etc
- Set the schedule value to a cron run time that does not exist for the cron job you want to disable. For example:
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nonamespaceschemalocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
<group id="default">
<job name="indexer_reindex_all_invalid" instance="Magento\Indexer\Cron\ReindexAllInvalid" method="execute">
<schedule>0 0 30 2 *</schedule>
</job>
</group>
</config>
Now save the file and run the commands below:
– php bin/magento setup:upgrade
– php bin/magento cache:clean
By doing this we are telling Magento to run the indexer_reindex_all_invalid
cron job at 00:00 on the 30th of February and this date never happens in reality. That means this cron job will never run in the future.
After following these steps, the cron job you disabled will not be executed anymore.
Keep in mind that disabling a cron job using the config file is a temporary solution. If you want to permanently disable a cron job, you can do so by uninstalling the module that defines the cron job or by removing the cron job from the module’s configuration file.
It is also important to note that disabling a cron job may have unintended consequences, as the cron job may be responsible for important tasks such as maintaining index tables or sending emails. Disabling a cron job should only be done after careful consideration of the possible impacts.
Also, read if you want to create a custom cron job in Magento 2 https://techurbane.com/how-to-create-cron-job-in-magento-2/.
For more info check Magento 2 documentation on Cron Job https://devdocs.magento.com/guides/v2.3/config-guide/cli/config-cli-subcommands-cron.html.
Hope you like this topic. If you find any issue with this topic then please comment below.