Skip to main content

django-dbcleanup

Easily monitor database usage - and clean it up (based on your django models)

This pluggable app provides:

  • visibility over database disk space usage for your models
  • command to remove unused tables and recover disk space
  • remove historical data from models that have any DateTimeField (configurable in the project settings.py)

Usage

model and admin view

dbcleanup.Table is an unmanaged model mapped to information tables in MySQL and PostgreSQL and added to django admin

image

command

dbcleanup is the management command that can be used (or scheduled) to remove unused

$ ./manage.py dbcleanup -h
usage: manage.py dbcleanup [-h] [-f] [-i] [-j {tables,history,analyze,migrations}] [--no-fk] [--version] [-v {0,1,2,3}] [--settings SETTINGS] [--pythonpath PYTHONPATH] [--traceback] [--no-color]
                           [--force-color] [--skip-checks]

Remove database tables that do not map to any models, such as when a django app is removed/disabled.

optional arguments:
  -h, --help            show this help message and exit
  -f, --force           Delete the items (instead of just listing them)
  -i, --interactive     Ask which items to delete, interactively
  -j {tables,history,analyze,migrations}, --just {tables,history,analyze,migrations}
                        Perform only a subset of actions
  --no-fk               Disable FOREIGNKEY_CHECK when DROPping tables - CAREFUL! use only if you are sure the constraints are not from a table in use (ie: circular dependencies between drop candidates)

Need to use --force or --interactive to actually perform changes, otherwise it'll be a dry run.
Covered actions are:

  • tables: remove database tables that do not map to any model (ie: when a app is removed from the project, there is no migration to delete the tables) - use settings.DBCLEANUP_REQUIRED_TABLES to whitelist tables that would otherwise be removed
  • history: remove old records for the models defined in settings.DBCLEANUP_HISTORY_MODELS (more below)
  • analyze: only for MySQL - force analyze on all the tables to update the row count and size estimates
  • migrations: remove migrations (from django_migrations table) that not in the project migration path (ie: after migration squashing and reset)

historical data

settings.DBCLEANUP_HISTORY_MODELS is a list of tuples where each tuple is (MODEL_NAME, DAYS_TO_KEEP, DATE_TIME_FIELD_NAME).

# someproject/settings.py
DBCLEANUP_HISTORY_MODELS = [
    ('notifications.notification', 365, 'time'),
    ...
]

With this setup, when ./manage.py dbcleanup -j history is executed, all notifications.Notification entries with time older than 365 days would be deleted.
History cleanup is skipped if it would cascade into other models (unless it's a multi-table model and it would cascade to the parent model).

Release files for django-dbcleanup 0.1.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-dbcleanup 0.1.5
File Size Uploaded
django_dbcleanup-0.1.5.tar.gz 10.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-dbcleanup 0.1.5
File Interpreter ABI Platform
django_dbcleanup-0.1.5-py3-none-any.whl Python 3 none any Details

Total release size: 21.9 kB

Release files / django_dbcleanup-0.1.5.tar.gz

Download URL django_dbcleanup-0.1.5.tar.gz
Size 10.8 kB
Tags Source
SHA-256 checksum
How to use checksums
cee97e0cbf2deff478a9dde7e3e2dc03d8405a2cb78d7d930e3c7f4bcc5f9ef1
BLAKE2b-256 checksum
How to use checksums
fff1e6e0b1a6ad08d89b2763f0b5db51f61d2c81350d4505a2bf00a6c1ce062c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.5

Release files / django_dbcleanup-0.1.5-py3-none-any.whl

Download URL django_dbcleanup-0.1.5-py3-none-any.whl
Size 11.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5e2a7b65f569b8dd3b0e50e870c1b22926669d1c3f50164566ec2b257a29bae7
BLAKE2b-256 checksum
How to use checksums
38bcdc5cf63684acd96a4694418b6c50898170a2d16c4554bb7c364277a43f0b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.5

Release history Release notifications | RSS feed

This release

0.1.5 This release

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.5

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page