Skip to main content

Periodically remove data from your Django app.

Project description

django-data-purger

Periodically remove data from your Django app.

Getting Started

  1. Install django-data-purger

Use Poetry to add the package

$ poetry add django-data-purger
  1. Add django_data_purger to INSTALLED_APPS

Update your INSTALLED_APP setting:

INSTALLED_APPS = [
    'django...',
    ...
    'django_data_purger',
]
  1. Create a data purger in the Django app you want to clean periodically

Example:

# data_purger.py
from django_data_purger.data_purger import DataPurger, PurgeResult
from app.models import DataModel
from datetime import datetime, timedelta

class PurgeDataModel(DataPurger):
    expected_delete_models = ("app.DataModel",)

    def run(self, *, now: datetime) -> list[PurgeResult]:
        old_threshold = now - timedelta(weeks=6)

        entries = DataModel.objects.filter(
            created_time__lte=old_threshold,
        )

        return self._delete_queryset_in_batch(
            entries, batch_size=DataPurger.BATCH_SIZE_LARGE
        )
  1. Register the data purger in the DATA_PURGERS setting

Add the purger to your settings:

DATA_PURGERS = [
    "app.data_purger.PurgeDataModel",
]
  1. Run the management command to purge old data

Configure this command to run periodically using a scheduler like cron:

$ python manage.py run_data_purgers --force

Settings

Setting name Type Default Description
DATA_PURGERS list[str] [] Array with import strings to data purgers in your project.

The DataPurger Class

The DataPurger class can be used to UPDATE or DELETE models. It runs within a transaction and ensures that updates or deletions are only applied to whitelisted models.

Update Model Instances

class PurgeDataModel(DataPurger):
    expected_update_models = ("app.DataModel",)

    def run(self, *, now: datetime) -> list[PurgeResult]:
        old_threshold = now - timedelta(weeks=6)

        entries = DataModel.objects.filter(
            created_time__lte=old_threshold,
        )

        return self._update_queryset_in_batch(
            entries, updates={"is_deleted": True}, batch_size=DataPurger.BATCH_SIZE_MEDIUM
        )

Delete Model Instances

class PurgeDataModel(DataPurger):
    expected_delete_models = ("app.DataModel",)

    def run(self, *, now: datetime) -> list[PurgeResult]:
        old_threshold = now - timedelta(weeks=6)

        entries = DataModel.objects.filter(
            created_time__lte=old_threshold,
        )

        return self._delete_queryset_in_batch(
            entries, batch_size=DataPurger.BATCH_SIZE_LARGE
        )

Planning for Model Instance Deletion

Models often depend on each other via ForeignKey or ManyToManyField relationships. It can be challenging to determine the correct order for deleting models without causing unexpected cascading deletions or errors from on_delete=models.PROTECT.

django-data-purger includes a tool to explore model dependencies. ✅ and 🛑 icons indicate whether a data purger for the model is already defined.

Example:

$ poetry run python manage.py calculate_model_dependencies --model app.DataModel
The following models depend on app.DataModel:
- ...

The following models depend on ...:
- ...

==============

2 models depend on app.DataModel.

==============

The models need to be deleted in the following order to safely delete app.DataModel:
(Models in the same batch can be deleted in any order.)

Batch 1:
-  ...
- 🛑 ...

Batch 2:
-  ...

Listing Models with Enabled Data Purgers

To view all models with a configured data purger:

$ python manage.py print_data_purging_enabled_tables --action delete

- app.DataModel

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_data_purger-0.2.9.tar.gz (10.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_data_purger-0.2.9-py3-none-any.whl (14.0 kB view details)

Uploaded Python 3

File details

Details for the file django_data_purger-0.2.9.tar.gz.

File metadata

  • Download URL: django_data_purger-0.2.9.tar.gz
  • Upload date:
  • Size: 10.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.12.3 Linux/6.11.0-1018-azure

File hashes

Hashes for django_data_purger-0.2.9.tar.gz
Algorithm Hash digest
SHA256 e41f906d1b4a78db9ce724dfe1546514ace9d96e07b41adca56f96cae1e5efc7
MD5 b7e3a68e54721e0c927ee924baf32b6c
BLAKE2b-256 23791febfb62c6a92bd74cf749d7ee1aa5ebfb297746886b32d01ced99ced8a7

See more details on using hashes here.

File details

Details for the file django_data_purger-0.2.9-py3-none-any.whl.

File metadata

  • Download URL: django_data_purger-0.2.9-py3-none-any.whl
  • Upload date:
  • Size: 14.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.12.3 Linux/6.11.0-1018-azure

File hashes

Hashes for django_data_purger-0.2.9-py3-none-any.whl
Algorithm Hash digest
SHA256 10b191900d05f141941354f6b8e68d9c428b4c5fc701dee9ec4d025213685571
MD5 d97bf6cb008187b03e0f3bc2c2ec1519
BLAKE2b-256 33141c1a5f91621b75878378b04e22db688540c064c08a1729c7a863c116cc51

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page