Hello Magento Friends,
Cron jobs are scheduled tasks that automate repetitive processes in your Magento 2 store. These tasks, like sending emails, updating product inventory, or running indexers, are crucial for maintaining the store’s functionality and performance. By default, Magento 2 comes with a set of built-in cron jobs. However, there might be times when you need to disable a specific default cron job programmatically.
Contents
Step 1: First, we need to create a “di.xml“ file inside our extension at the following path
app\code\Vendor\Extension\etc\di.xml
Now, add the code as follows
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\Cron\Model\Config"> <plugin name="disable_default_cron_jobs" type="Vendor\Extension\Plugin\Disabledefaultcronjobs"/> </type> </config>
Step 2: After that, we need to create a “Disabledefaultcronjobs.php” file inside our extension at the following path
app\code\Vendor\Extension\Plugin\Disabledefaultcronjobs.php
And add the code as given below
<?php namespace Vendor\Extension\Plugin; class Disabledefaultcronjobs { public function aroundGetJobs(\Magento\Cron\Model\Config $subject, callable $proceed) { // Get all configured jobs $jobs = $proceed(); // List of jobs to keep (your custom jobs) $allowedJobs = [ 'backend_clean_cache', // Replace with your custom job name ]; // Filter out Magento default jobs foreach ($jobs as $group => $groupJobs) { foreach ($groupJobs as $jobName => $jobConfig) { if (!in_array($jobName, $allowedJobs)) { unset($jobs[$group][$jobName]); } } } return $jobs; } }
Note: All default Magento cron jobs are disabled except those specified in the $allowedJobs array.
Disabling a default cron job in Magento 2 can be useful for optimizing your store’s performance or preventing conflicts with custom code or third-party extensions. Using a custom module and plugin ensures you avoid directly modifying core Magento files, keeping your store more maintainable and upgrade-safe.
By following the steps outlined in this tutorial, you can easily disable unnecessary cron jobs programmatically in Magento 2.
Happy Coding!
In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…
Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…
The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…
Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…
If you are a Shopify admin, using a Shopify Balance Account for your business revenue…
Running an eCommerce business can be incredibly demanding, leaving entrepreneurs little time to focus on…