Here is a fix that works with Celery's default scheduler. A periodic task is doing something it shouldn't, so you delete its entry from CELERY_BEAT_SCHEDULE, deploy, restart Beat, and it stops.
We did exactly that once. A task was making about 10,000 pointless API calls a day, we removed it from settings, and that was the end of it. Months later we moved Beat's schedule into the database with django-celery-beat. Then it hit us that the same fix would have changed nothing there.
Why the row outlives the entry
DatabaseScheduler copies your settings into the PeriodicTask table every time Beat starts. This is the whole sync in django-celery-beat 2.9.0:
# django_celery_beat/schedulers.py
def setup_schedule(self):
self.install_default_entries(self.schedule)
self.update_from_dict(self.app.conf.beat_schedule)
# ModelEntry.from_entry, called once per entry
obj, created = PeriodicTask._default_manager.update_or_create(
name=name, defaults=cls._unpack_fields(**entry),
)
It adds and it updates. Nothing deletes anything. An entry you removed from settings leaves its row behind, still enabled, and Beat keeps firing it on the old schedule. Nothing in the logs mentions it, and the deploy looks like it worked.
This isn't news. Issue #248 ("Removing tasks from celery_beat config doesn't remove them from database") has been open since 2019. The usual answers are a hand-written cleanup function, a trip to the admin, or deleting every row and letting Beat recreate them.
The obvious fix, and the five ways it goes wrong
The obvious fix: after setup_schedule(), delete every row whose name isn't in the schedule. That is about six lines of code. Writing it is where we found the edge cases.
1. Celery's own entry. When results expire, Beat installs celery.backend_cleanup by itself. It isn't in your settings, so a naive diff deletes it on every start, and Beat puts it back on the next one.
2. An empty schedule. Your settings fail to load and beat_schedule comes back {}. Taken literally, that schedule means "delete every row", and all scheduled work stops. Ours refuses to prune an empty schedule and logs a warning instead.
3. Disabling instead of deleting. Flipping enabled=False feels safer than a delete. It's worse. The stock sync never writes enabled unless your entry sets it, so a row disabled by mistake stays disabled through every later restart, and that task is silently dead forever. A row deleted by mistake comes back from settings at the next start.
The same fact is why pruning must never touch enabled at all. Unticking Enabled in the admin is the emergency switch that needs no deploy, and it should stay that way.
4. One-off tasks. Rows with one_off=True are usually created from code for a single run. They were never meant to be in settings, so they're kept by default.
5. The schedule Beat already holds. Deleting the row isn't enough on its own. The running Beat has the entry in memory and will fire it until its next full re-read of the table. So the pruned names also have to come out of scheduler._schedule, and PeriodicTasks.update_changed() has to be called so other processes notice.
Tested against a real Beat
Unit tests with a mocked scheduler would pass while the real thing broke. One test in the suite migrates a SQLite database and leaves a stale row in it. It then starts an actual celery beat subprocess with the scheduler named in settings, and checks that the row is gone and named in the log. CI runs on the oldest and the newest supported Python, Django, Celery and django-celery-beat.
Using it
We packaged the scheduler as django-celery-beat-prune, MIT licensed:
pip install django-celery-beat-prune
# settings.py
CELERY_BEAT_SCHEDULER = "django_celery_beat_prune.PruningScheduler"
At the next Beat start, each row with no entry in the schedule is removed and named in the log:
[WARNING] Removing 1 periodic task(s) with no entry in the beat schedule: refresh-statuses
If you also create tasks in the admin or from code, protect them by name or by pattern:
CELERY_BEAT_PRUNE_KEEP = ["manual-*", "nightly-export"]
To see what would go before letting it delete anything:
python manage.py prune_periodic_tasks --dry-run
Source and issues: github.com/theloops-live/django-celery-beat-prune.
I run TheLoops, which streams pre-recorded video to YouTube, Twitch and Kick around the clock. Its periodic work runs on Celery Beat, which is where this came from.
Top comments (0)