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.11.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.11-py3-none-any.whl (14.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_data_purger-0.2.11.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.11.tar.gz
Algorithm Hash digest
SHA256 24fecbd587170c3c395aea01cd99b707673fd311a90c33cdca06250c988ca0d7
MD5 704fdacd6051eb3d0af85ce1b9636e30
BLAKE2b-256 42983d5e6ba129488fc358bde76241baceab2ce36c8ddc914b72292ed91950e1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: django_data_purger-0.2.11-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.11-py3-none-any.whl
Algorithm Hash digest
SHA256 ba7c18eed10867fe782ebb9b58bba8381ff01d49d2e00cc804412c5c1839981b
MD5 5a8eb4b03f9ae687cb94747b4f3ed4ab
BLAKE2b-256 a3da67b5b09fbdec1ca55be2b131a57e2237dea5e177d602bba9e26f64273a90

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