✅ Overview
Aspect |
@api.autovacuum |
Cron Job (ir.cron) |
| Purpose | Background maintenance & cleanup tasks | Scheduled operations or business processes |
| Configuration | Hardcoded (decorator-based, no UI control) | Configurable via UI (Settings > Technical) |
| Flexibility | Limited (no custom timing via UI) | Highly flexible (timing, retries, logging) |
| Use Cases | Auto-cleaning expired data, temp records, etc. | Sending emails, scheduled reports, syncing |
| Logging | ❌ No logging by default | ✅ Yes, with last run, next run, traceback |
| Multi-database aware | ✅ Yes | ✅ Yes |
| Triggered by | odoo-bin’s internal vacuum thread | ir.cron scheduler in Odoo |
A special Odoo decorator used for automatic, low-level cleanup tasks, typically registered internally by Odoo core module
Example
from odoo import models, api
from datetime import timedelta
class SessionGC(models.AbstractModel):
_name = ‘session.gc’
_description = ‘Session Garbage Collector’
@api.autovacuum
def _gc_sessions(self):
expired = self.env[‘session’].search([
(‘last_seen’, ‘<‘, fields.Datetime.now() – timedelta(days=7))
])
expired.unlink()
- No need to define ir.cron.
- Executed internally when vacuum thread runs.
- Used in modules like base, mail, etc.
📌 What is ir.cron?
A model used to define scheduled server actions in Odoo.
Example
<record id=”model_task_auto_close” model=”ir.cron”>
<field name=”name”>Auto Close Tasks</field>
<field name=”model_id” ref=”model_project_task”/>
<field name=”state”>code</field>
<field name=”code”>model._auto_close_tasks()</field>
<field name=”interval_number”>1</field>
<field name=”interval_type”>days</field>
<field name=”numbercall”>-1</field>
</record>
- Can be managed from the UI.
- Has logs, failure notifications, retry logic.
- Better suited for business-level automation.
✅ Summary Table
Criteria |
@api.autovacuum |
Cron Jobs (ir.cron) |
| Intended for | Internal cleanup/garbage collection | Business automation |
| Configurable by user | ❌ No | ✅ Yes |
| Logging & monitoring | ❌ No | ✅ Yes |
| Flexibility (timing, code) | ❌ Limited | ✅ High |
| Common use in Odoo | Expiring tokens, logs, sessions | Reports, auto follow-ups |
🔍 When to Use What?
Use Case |
Recommended Approach |
| Remove expired auth tokens | @api.autovacuum |
| Auto-close stale tickets every night | ir.cron |
| Cleanup expired job queue entries | @api.autovacuum |
| Notify users of upcoming deadlines | ir.cron |
| Recompute dynamic KPIs nightly | ir.cron |