The Django Database Purge management command is a tool for efficiently removing unwanted records from your Django project's database based on a specified retention policy. This command helps you keep your database clean and optimized by permanently deleting records that are no longer needed.
- Flexible Retention Policy: Define your own retention policy to determine which records should be purged from the database.
- Efficient Data Management: Easily manage the size of your database by removing outdated or unnecessary records.
- Customizable: Adapt the command to suit your project's specific requirements and database structure.
- Safe: Built-in safeguards to prevent accidental data loss, ensuring that only the intended records are purged.
- Install django-db-purge by running:
pip install django-db-purge
- Include 'dbpurge' in your INSTALLED_APPS settings.
- Locate the
db_purge.py
file in themanagement/commands
directory of the Django dbpurge app. - Add your own values to the retention policies dictionary in the
db_purge.py
file, based on your requirements. Below is a guide on how to set up the retention policies:
- Description: Name of the Django app containing the model.
- Example:
my_django_app
- Description: Name of the Django model from which records will be deleted.
- Example:
MyModel
- Description: Name of the column in the model that contains the timestamp or datetime field used for determining the age of records.
- Example:
created_at
- Description: Time duration in seconds for which records will be retained before deletion.
- Example:
2592000
(for 30 days)
retention_policies = [
{
'app_name': 'my_django_app',
'model_name': 'MyModel',
'time_based_column_name': 'created_at',
'data_retention_num_seconds': 2592000, # 30 days in seconds
},
# Add more retention policies as needed
]
- Then, either periodically call the dbpurge management command (e.g., via a system cronjob), or install and configure django-cron.
Contributions are welcome! If you encounter any issues or have suggestions for improvements, please submit an issue or pull request on GitHub.
This project is licensed under the MIT License.