Package: django-tables-cleaner
tables_cleaneris a Django app used to remove oldest records from specific db tables.It is intended to be used in production to keep under control the size of growing
tables containing temporary data (used for logging, auditing, …), but preserving
the most recent records according to the constraints assigned by design (seeTABLES_CLEANER_TABLESsetting).Quick startInstallation:pip install django-tables-cleaner

or

pip install git+https://github.com/morlandi/django-tables-cleanerAdd “tables_cleaner” to your INSTALLED_APPS setting like this:INSTALLED_APPS = [
    ...
    'tables_cleaner',
]Run the management command periodically (i.e. with cron)python manage.py clean_tablesUsageThe first option is torecall periodically the management commandclean_tables, for example via cron:usage: manage.py clean_tables [-h] [--database DATABASE] [-d] [--vacuum]
                              [--version] [-v {0,1,2,3}] [--settings SETTINGS]
                              [--pythonpath PYTHONPATH] [--traceback]
                              [--no-color]

optional arguments:
  -h, --help            show this help message and exit
  --database DATABASE   Nominates a specific database to load fixtures into.
                        Defaults to the "default" database.
  -d, --dry-run         Don't actually delete records (default: False)
  --vacuum              Run VACUUM after deletion
  --version             show program's version number and exit
  -v {0,1,2,3}, --verbosity {0,1,2,3}
                        Verbosity level; 0=minimal output, 1=normal output,
                        2=verbose output, 3=very verbose output
  --settings SETTINGS   The Python path to a settings module, e.g.
                        "myproject.settings.main". If this isn't provided, the
                        DJANGO_SETTINGS_MODULE environment variable will be
                        used.
  --pythonpath PYTHONPATH
                        A directory to add to the Python path, e.g.
                        "/home/djangoprojects/myproject".
  --traceback           Raise on CommandError exceptions
  --no-color            Don't colorize the command output.Or, when using a different scheduling strategy (for example withdjango-cron)you can call from Python code the following function:clean_tables(logger=None,dry_run=False)For example:importtables_cleanertables_cleaner.clean_tables()Finally, for very specific needs, you canrecall the real workhorse directly:defclean_table(model_name,keep_records,keep_since_days,keep_since_hours,get_latest_by=None,logger=None,dry_run=False)which act on a single table, and doesn’t require any setting.SettingsTABLES_CLEANER_TABLESThe list of models to be cleaned;options:keep_records: n. of most recent records to be preserved; 0=unusedkeep_since_days: always preserve records more recent than this; 0=unusedkeep_since_hourse: always preserve records more recent than this; 0=unusedExample:TABLES_CLEANER_TABLES = [
    {
        'model_name': 'backend.log',
        'keep_records': 1000,
        'keep_since_days': 1,
        'keep_since_hours': 0,
    }, {
        'model_name': 'tasks.updatedevicetask',
        'keep_records': 100,
        'keep_since_days': 0,
        'keep_since_hours': 12,
        'get_latest_by': 'created',
    },
]get_latest_byattribute is optional; if not supplied, Model’s Meta get_latest_by
is used instead.Vacuum strategy“VACUUM” is optionally executed as a final activity (’–vacuum’).Since version v0.1.0, we opted to use “VACUUM” instead of “VACUUM FULL”, since that
seems more appropriate for ordinary database maintenance, for the following reasons:it’s available for Postgresql and Sqlite (and, hopefully, for other databases too)database owners are allowed to vacuum all tables in their databasesan exclusive lock is not requiredit’s potentially much fasterPostgreSQL documentation explicitly states thatThe FULL option is not recommended for routine use;
see:VACUUM — garbage-collect and optionally analyze a databaseThanks to John Vandenberg for bringing my attention to this.FileFields and ImageFieldsRemoving rows in the database when the Model contains one or more FileField or
ImageField is not enough, since some garbage is left in the Media folder.I normally usedjango-cleanupto cope with this.Does it work?A few unit tests have been provided.Prepare the virtual environment as follows:python -m pip install -r requirements.txtthen:./runtests.pyor:coverage run --source='.' runtests.py
coverage reportReferencesUsing the Django test runner to test reusable applicationsAdvanced tutorial: How to write reusable appsTODO: Running tests using toxLicenseThis code is distributed under the terms of the MIT license.Historyv0.1.4Example project addedRefactoring: app logic moved to standalone Python functionsUnit tests addedv0.1.3Python and Django classifiers added to setup.pyv0.1.2apply vacuum only when supported by db enginesv0.1.1published on PyPIv0.1.0prepare for publishing on PyPIuse “VACUUM” instead of “VACUUM FULL”dry run option renamed as “-d” (was “-n”)v0.0.5Fix for Django 2.x: call super() from Command.__init__() as requiredv0.0.4Customizable ‘get_latest_by’ attributeRemove EmptyResultSet import which is not available in older versions of Djangov0.0.3Setup fixv0.0.2First working implementationv0.0.1Initial setup