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

Handling Forms and Data in Shopify Remix: useSubmit vs. useFetcher

In Shopify Remix, managing form submissions and data fetching is crucial for building interactive and…

12 hours ago

SEO and Digital Marketing for Magento Stores

When positioning oneself in the constantly developing field of internet sales, it is critical to…

16 hours ago

Emerging Shopify Trends That Student Entrepreneurs Should Know About

One major challenge student entrepreneurs encounter is difficulty balancing academics and business. Most find themselves…

16 hours ago

How to Setup Vite in Shopify Remix App?

In this article, we will learn how to set up Vite in the Shopify remix…

2 days ago

Magento 2: How to Add View Button in Admin Grid to Open a View Page in Slide Window

Hello Magento Friends, In Magento 2, customizations to the admin panel can significantly enhance the…

3 days ago

Magento 2: How to Observe the Multi-shipping Order Creation Event

Hello Magento Friends, Magento 2 provides a robust event-driven architecture that allows developers to observe…

6 days ago