Skip to main content

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

Metadata

Release files for django-data-purger 0.2.11

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-data-purger 0.2.11
File Size Uploaded Provenance
django_data_purger-0.2.11.tar.gz 10.4 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for django-data-purger 0.2.11
File Interpreter ABI Platform
django_data_purger-0.2.11-py3-none-any.whl Python 3 none any Details

Total release size: 24.6 kB

Release files / django_data_purger-0.2.11.tar.gz

Download URL django_data_purger-0.2.11.tar.gz
Size 10.4 kB
Tags Source
SHA-256 checksum
How to use checksums
24fecbd587170c3c395aea01cd99b707673fd311a90c33cdca06250c988ca0d7
BLAKE2b-256 checksum
How to use checksums
42983d5e6ba129488fc358bde76241baceab2ce36c8ddc914b72292ed91950e1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.3.4 CPython/3.12.3 Linux/6.17.0-1010-azure

Release files / django_data_purger-0.2.11-py3-none-any.whl

Download URL django_data_purger-0.2.11-py3-none-any.whl
Size 14.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ba7c18eed10867fe782ebb9b58bba8381ff01d49d2e00cc804412c5c1839981b
BLAKE2b-256 checksum
How to use checksums
a3da67b5b09fbdec1ca55be2b131a57e2237dea5e177d602bba9e26f64273a90
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.3.4 CPython/3.12.3 Linux/6.17.0-1010-azure

Security for django-data-purger 0.2.11

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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