Setting up a scheduled task


6.0.0 or newer

Table of contents


Quite often one might want to run any type of code on a regular basis, e.g. to clean up very old entries every once in a while, automatically. Formerly known as "Cronjobs", Shopware 6 supports a ScheduledTask for this.

A ScheduledTask and its respective ScheduledTaskHandler are registered in a plugin's services.xml. For it to be found by Shopware 6 automatically, you need to place the services.xml file in a Resources/config/ directory, relative to the location of your plugin's base class. The path could look like this: <plugin root>/src/Resources/config/services.xml, if you were to place your plugin's base class in <plugin root>/src.

Registering scheduled task in the DI container

Here's an example services.xml containing a new ScheduledTask as well as a new ScheduledTaskHandler:

<?xml version="1.0" ?>

<container xmlns=""

        <service id="Swag\ScheduledTaskPlugin\ScheduledTask\MyTask">
            <tag name="shopware.scheduled.task" />

        <service id="Swag\ScheduledTaskPlugin\ScheduledTask\MyTaskHandler">
            <argument type="service" id="scheduled_task.repository" />
            <tag name="messenger.message_handler" />

Note the tags required for both the task and its respective handler. Your custom task will now be saved into the database once your plugin is activated.

ScheduledTask and its handler

As you might have noticed, the services.xml file tries to find both the task itself as well as the new task handler in a directory called ScheduledTask. This naming is up to you, Shopware 6 decided to use this name though.

Here's the mentioned example ScheduledTask file in <plugin root>/src/ScheduledTask/:

<?php declare(strict_types=1);

namespace Swag\ScheduledTaskPlugin\ScheduledTask;

use Shopware\Core\Framework\MessageQueue\ScheduledTask\ScheduledTask;

class MyTask extends ScheduledTask
    public static function getTaskName(): string
        return 'vendor_prefix.my_task';

    public static function getDefaultInterval(): int
        return 300; // 5 minutes

Make sure to add a vendor prefix to your custom task, to prevent collisions with other plugin's scheduled tasks.

Following will be the respective task handler:

<?php declare(strict_types=1);

namespace Swag\ScheduledTaskPlugin\ScheduledTask;

use Shopware\Core\Framework\MessageQueue\ScheduledTask\ScheduledTaskHandler;

class MyTaskHandler extends ScheduledTaskHandler
    public static function getHandledMessages(): iterable
        return [ MyTask::class ];

    public function run(): void
        echo 'Do stuff!';

Now every five minutes, your task will be executed and it will print an output every time now.


There's a GitHub repository available, containing this example source. Check it out here.

Was this article helpful?