'django-celery-results are not expiring even though CELERY_RESULT_EXPIRES is set

I have an every minute task running on celery beat and I'm using rabbimq as a broker and have PostgreSQL as a database.

As my task is running every minute and I'm storing task results in the database, my database size is increasing continuously day by day. I was looking into the expiration of stored task results. As per celery documentation, there is a CELERY_RESULT_EXPIRES config that uses celery beat to do a cleanup. Seems like it's not happening as expected.

What am I missing or doing wrong? Any pointers would be really great help.

Following are my celery settings defined in my Django settings.py

CELERY_BROKER_URL = os.getenv('CELERY_BROKER_URL', 'amqp://localhost')
CELERY_RESULT_BACKEND = 'django-db'
CELERY_CACHE_BACKEND = 'django-cache'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_ENABLE_UTC = False
CELERY_TIMEZONE = 'UTC'
CELERY_CREATE_MISSING_QUEUES = False
CELERY_DEFAULT_QUEUE = 'celery'
CELERY_TASK_TRACK_STARTED = True
CELERY_SEND_EVENTS = True
CELERY_SEND_SENT_EVENT = True
CELERY_WORKER_MAX_TASKS_PER_CHILD = 25
CELERY_TASK_REJECT_ON_WORKER_LOST = True
CELERYD_TIME_LIMIT = 60
CELERYD_SOFT_TIME_LIMIT = 50
CELERY_ACKS_LATE = True
CELERYD_PREFETCH_MULTIPLIER = 1
CELERY_RESULT_EXPIRES = 1 * 60

Following is my celery.py

import django
import logging
from celery import Celery
from celery.schedules import crontab
from kombu import Exchange, Queue

log = logging.getLogger(__name__)

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'xxx.settings')
django.setup()

app = Celery('xxx')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()

app.conf.task_queues = (
    Queue('celery', Exchange('celery', type='direct'), routing_key='celery'),
    Queue('project_indicators', Exchange('high', type='direct'), routing_key='high'),
)

app.conf.beat_schedule = {
    'every-minute-task': {
        'task': 'every_minute_task',
        'schedule': crontab(),
    },
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source