django-celery-beat-prune
Delete a task from CELERY_BEAT_SCHEDULE and, with django-celery-beat, it keeps running. This scheduler makes settings the schedule again. MIT licensed.
With Celery's file-based scheduler, deleting an entry from the beat schedule and redeploying is how a periodic task is stopped. Move the schedule into the database with django-celery-beat's DatabaseScheduler and that stops being true. At every Beat start the scheduler copies settings into the PeriodicTask table, but it only ever adds and updates:
# django_celery_beat/schedulers.py (2.9.0), abridged
def setup_schedule(self):
self.install_default_entries(self.schedule)
self.update_from_dict(self.app.conf.beat_schedule) # update_or_create, per entry
# ...and nothing anywhere deletes a row whose entry is gone.So the removed entry's row stays, enabled, and the task keeps firing on its old schedule. Nothing in the logs mentions it, and the deploy looks like it worked. The question has come up on Stack Overflow for years; the answers are a hand-written cleanup snippet or a trip to the admin.
Install
pip install django-celery-beat-prune
# settings.py CELERY_BEAT_SCHEDULER = "django_celery_beat_prune.PruningScheduler"
That is the whole setup. At the next Beat start, every row with no entry in CELERY_BEAT_SCHEDULE is removed, and each one is named in the log:
[WARNING] Removing 1 periodic task(s) with no entry in the beat schedule: refresh-statuses
Python 3.10+, Django 4.2+, Celery 5.3+, django-celery-beat 2.5+. Add "django_celery_beat_prune" to INSTALLED_APPS if you want the management command below.
The choices it makes
- Deleted, not disabled.
update_or_createnever writesenabled, so a row disabled by mistake would stay disabled through every later restart, and that task would be silently dead. A row deleted by mistake comes back from settings at the next start. - The admin's Enabled switch still works. Pruning never touches
enabled, so turning a task off in the admin stays the emergency lever that needs no deploy. - An empty schedule prunes nothing. An empty
CELERY_BEAT_SCHEDULEalmost always means settings failed to load. Taken literally, it would delete every row and stop all scheduled work. - A pruned task cannot fire one last time. It is also dropped from the schedule the running Beat already holds, which it would otherwise use until its next full re-read of the table.
- Kept on purpose: Celery's own
celery.backend_cleanup, which Beat recreates anyway, and rows withone_off=True, which code creates for a single run.
Tasks created in the admin
If you also create periodic tasks by hand or from code, protect them by name or by shell-style pattern:
CELERY_BEAT_PRUNE_KEEP = ["manual-*", "nightly-export"]
CELERY_BEAT_PRUNE_DRY_RUN = True logs what would be removed and removes nothing. CELERY_BEAT_PRUNE_ONE_OFF = True prunes one-off rows too.
Look before you prune
The first time, see what would go without restarting Beat:
python manage.py prune_periodic_tasks --dry-run python manage.py prune_periodic_tasks --keep "manual-*"
Tested against a real Beat
Besides the unit tests, the suite migrates a SQLite database, leaves a stale row in it, starts an actual celery beat process 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 versions of Python, Django, Celery and django-celery-beat.
Why we wrote it
TheLoops streams pre-recorded video to YouTube, Twitch and Kick around the clock, and its periodic work runs on Celery Beat. We once stopped a task by commenting it out of settings, because it was making about 10,000 pointless API calls a day. When we later moved the schedule into Postgres, we realised that on a plain DatabaseScheduler that fix would have changed nothing. We wrote a pruning scheduler for our own Beat then; this package is that scheduler, taken out of our codebase and given the options another project would need.
Source and issues are on GitHub. More engineering notes, including channels-jwt-subprotocol.