Cleaning up the log | laravel-activitylog | Spatie

 SPATIE

  Laravel Activity Log
=======================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-activitylog](https://spatie.be/docs/laravel-activitylog/v5)  Basic-usage  Cleaning up the log

 Version   v5   v4   v3   v2   v1

 Other versions for crawler [v5](https://spatie.be/docs/laravel-activitylog/v5) [v4](https://spatie.be/docs/laravel-activitylog/v4) [v3](https://spatie.be/docs/laravel-activitylog/v3) [v2](https://spatie.be/docs/laravel-activitylog/v2) [v1](https://spatie.be/docs/laravel-activitylog/v1)

- [ Introduction ](https://spatie.be/docs/laravel-activitylog/v5/introduction)
- [ Support us ](https://spatie.be/docs/laravel-activitylog/v5/support-us)
- [ Requirements ](https://spatie.be/docs/laravel-activitylog/v5/requirements)
- [ Installation and Setup ](https://spatie.be/docs/laravel-activitylog/v5/installation-and-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-activitylog/v5/questions-and-issues)
- [ Changelog ](https://spatie.be/docs/laravel-activitylog/v5/changelog)
- [ Upgrading ](https://spatie.be/docs/laravel-activitylog/v5/upgrading)
- [ About us ](https://spatie.be/docs/laravel-activitylog/v5/about-us)

Basic usage
-----------

- [ Logging activity ](https://spatie.be/docs/laravel-activitylog/v5/basic-usage/logging-activity)
- [ Cleaning up the log ](https://spatie.be/docs/laravel-activitylog/v5/basic-usage/cleaning-up-the-log)

Advanced usage
--------------

- [ Logging model events ](https://spatie.be/docs/laravel-activitylog/v5/advanced-usage/logging-model-events)
- [ Define causer for runtime ](https://spatie.be/docs/laravel-activitylog/v5/advanced-usage/define-causer-for-runtime)
- [ Using placeholders ](https://spatie.be/docs/laravel-activitylog/v5/advanced-usage/using-placeholders)
- [ Using multiple logs ](https://spatie.be/docs/laravel-activitylog/v5/advanced-usage/using-multiple-logs)
- [ Disabling logging ](https://spatie.be/docs/laravel-activitylog/v5/advanced-usage/disabling-logging)
- [ Customizing actions ](https://spatie.be/docs/laravel-activitylog/v5/advanced-usage/customizing-actions)
- [ Before logging hook ](https://spatie.be/docs/laravel-activitylog/v5/advanced-usage/before-logging-hook)
- [ Buffering activities ](https://spatie.be/docs/laravel-activitylog/v5/advanced-usage/buffering)
- [ Log Options ](https://spatie.be/docs/laravel-activitylog/v5/advanced-usage/log-options)
- [ Causer Resolver ](https://spatie.be/docs/laravel-activitylog/v5/advanced-usage/causer-resolver)

 Cleaning up the log
===================

###  On this page

1. [ Define the log to clean ](#content-define-the-log-to-clean)
2. [ Overwrite the days to keep per call ](#content-overwrite-the-days-to-keep-per-call)
3. [ MySQL: rebuild index and reclaim space after clean ](#content-mysql-rebuild-index-and-reclaim-space-after-clean)

After using the package for a while, you might have recorded a lot of activity. This package provides an artisan command `activitylog:clean` to clean the log.

Running this command will result in the deletion of all recorded activity that is older than the number of days specified in the `clean_after_days` key of the config file.

You can leverage Laravel's scheduler to run the clean up command now and then.

```
php artisan activitylog:clean
```

```
// routes/console.php

use Illuminate\Support\Facades\Schedule;

Schedule::command('activitylog:clean --force')->daily();
```

The `--force` flag is needed because the command will otherwise ask you to confirm the action when running in production. This is to prevent accidental data loss.

Define the log to clean
-----------------------------------------------------------------------------------------------------------------------------

If you want to clean just one log, you can define it as a command argument. It will filter the `log_name` attribute of the `Activity` model.

```
php artisan activitylog:clean my_log_channel
```

Overwrite the days to keep per call
-----------------------------------------------------------------------------------------------------------------------------------------------------------------

You can define the days to keep for each call as a command option. This will overwrite the config for this run.

```
php artisan activitylog:clean --days=7
```

MySQL: rebuild index and reclaim space after clean
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

After cleaning, the database table might still use more space than expected. You can run the following MySQL commands to reclaim space:

```
OPTIMIZE TABLE activity_log;
```

or

```
ANALYZE TABLE activity_log;
```

\*These SQL operations will lock read/write access to the database. Only run them when the server is in maintenance mode.
