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.10.tar.gz (10.4 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.10-py3-none-any.whl (14.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_data_purger-0.2.10.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.4 CPython/3.12.3 Linux/6.17.0-1010-azure

File hashes

Hashes for django_data_purger-0.2.10.tar.gz
Algorithm Hash digest
SHA256 c2549694e15dc2e1f843d426eeb1e9d9a170fcc27205f2f89389452bcdd71a89
MD5 b0072cad925f9523fdb04f2fffee0330
BLAKE2b-256 f40fcf02cd553b71af923300ca40da433dcbd6882b53d8b9492abc08ca4f3208

See more details on using hashes here.

File details

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

File metadata

  • Download URL: django_data_purger-0.2.10-py3-none-any.whl
  • Upload date:
  • Size: 14.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.4 CPython/3.12.3 Linux/6.17.0-1010-azure

File hashes

Hashes for django_data_purger-0.2.10-py3-none-any.whl
Algorithm Hash digest
SHA256 5ce2f6f4320898db8a8b2b2bdad0071a03fb70e2f2db42f50132ca9c558e8410
MD5 9e7b981855f8393645d87e94dbb4ef1a
BLAKE2b-256 ee1d6aebfcf32de0f2a056a2dccb7c33cfc3b5e6b020c5536dcc24138b428c22

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