# Data Retention User Guide

## Introduction

The data retention functionality enables you to delete old plugin data after a specified amount of days.

You can specify the data retention policy for each plugin via October's backend settings.

> Important: To automatically delete old data make sure you have set up the Task Scheduler correctly.

## Table of Contents:

1. Register your plugin

2. Cleanup method

3. Cleanup command

### Register your plugin

To register your plugin you have to listen for the offline.gdpr::cleanup.register event in your Plugin's boot method.

    public function boot()

    {

      \Event::listen('offline.gdpr::cleanup.register', function () {

        return [

          'id'     => 'your-contact-form-plugin',

          'label'  => 'Custom Contact Form Plugin',

          'models' => [

            [

              'label'   => 'Contact form messages',

              'comment' => 'Delete logged contact form messages',

              'class'   => MessageLog::class,

            ],

            [

              'label'   => 'SPAM-Messages',

              'comment' => 'Delete blocked SPAM messages',

              'closure' => function (Carbon $deadline, int $keepDays) {

                // Delete your old data here

              },

            ],

          ],

        ];

      });

    }

You have to specify the following data:

Key | Information
--- | ---
id | A unique identifier of your plugin.
label | A human readable label for your plugin.
models | An array of all your data collecting models.

As models you have to specify an array with the following data:

Key | Information
--- | ---
label | A human readable label for the backend switch form widget.
comment | A human readable comment for the backend switch form widget.
closure | A closure that is called when the cleanup job is run.
class | A model class that defines a gdprCleanup method.

You have to specify either a closure or a class value. If both are specified the closure value will be used.

### Cleanup method

You can either specify a closure or a model class that defines a gdprCleanup method. Both have the same signature:

    public function gdprCleanup(Carbon $deadline, int $keepDays)

    {

      self::where('created_at', '<', $deadline)->each(function (self $item) {

        $item->delete();

      });

      // or

      // self::where('created_at', '<', $deadline)->delete();

    }

This method is called whenever the cleanup job is run. $deadline contains a Carbon instance. All data older than this date has to be deleted. $keepDays contains the number of days that $deadline is in the past.

Make sure to use an each/delete loop if your model makes use of deleting/deleted model events.

### Cleanup command

You can trigger the cleanup on demand via

    php artisan gdpr:cleanup