How To

How to Disable Default Cron Job Programmatically in Magento 2?

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.

Reasons to Disable Default Cron Jobs

  • Conflict with Custom Cron Jobs: If you’ve created custom cron jobs for specific tasks, they might conflict with default jobs, leading to unexpected behavior.
  • Performance Optimization: Certain default cron jobs might not be necessary for your store’s current needs, and disabling them can improve performance.
  • Debugging: Disabling specific cron jobs can help isolate issues and troubleshoot problems.

Steps to Disable Default Cron Job Programmatically in Magento 2:

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.

Conclusion:

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!

Click to rate this post!
[Total: 0 Average: 0]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

Recent Posts

NodeJS | Callback Function

In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…

8 hours ago

How to Show SKU in Order Summary in Magento 2?

Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…

2 days ago

Best Colors to Use for CTA Buttons

The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…

4 days ago

Magento 2: How to Save Custom Field Value to quote_address for Multi-Shipping Orders

Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…

5 days ago

Best Beginners Guide to Shopify Balance Account

If you are a Shopify admin, using a Shopify Balance Account for your business revenue…

5 days ago

8 Best Social Login Apps for Shopify Store in 2024

Running an eCommerce business can be incredibly demanding, leaving entrepreneurs little time to focus on…

5 days ago