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/v4)  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/v4/introduction)
- [ Support us ](https://spatie.be/docs/laravel-activitylog/v4/support-us)
- [ Requirements ](https://spatie.be/docs/laravel-activitylog/v4/requirements)
- [ Installation and Setup ](https://spatie.be/docs/laravel-activitylog/v4/installation-and-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-activitylog/v4/questions-and-issues)
- [ Changelog ](https://spatie.be/docs/laravel-activitylog/v4/changelog)
- [ Upgrading ](https://spatie.be/docs/laravel-activitylog/v4/upgrading)
- [ About us ](https://spatie.be/docs/laravel-activitylog/v4/about-us)

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

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

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

- [ Logging model events ](https://spatie.be/docs/laravel-activitylog/v4/advanced-usage/logging-model-events)
- [ Manipulate changes array ](https://spatie.be/docs/laravel-activitylog/v4/advanced-usage/manipulate-changes-array)
- [ Batch Logs ](https://spatie.be/docs/laravel-activitylog/v4/advanced-usage/batch-logs)
- [ Define causer for runtime ](https://spatie.be/docs/laravel-activitylog/v4/advanced-usage/define-causer-for-runtime)
- [ Using placeholders ](https://spatie.be/docs/laravel-activitylog/v4/advanced-usage/using-placeholders)
- [ Using multiple logs ](https://spatie.be/docs/laravel-activitylog/v4/advanced-usage/using-multiple-logs)
- [ Disabling logging ](https://spatie.be/docs/laravel-activitylog/v4/advanced-usage/disabling-logging)

API
---

- [ Log Options ](https://spatie.be/docs/laravel-activitylog/v4/api/log-options)
- [ Log Batch ](https://spatie.be/docs/laravel-activitylog/v4/api/log-batch)
- [ Causer Resolver ](https://spatie.be/docs/laravel-activitylog/v4/api/causer-resolver)
- [ Event Bag ](https://spatie.be/docs/laravel-activitylog/v4/api/event-bag)

      You are viewing the documentation for **an older version** of this package. You can check the version you are using with the following command:

 `                                    composer show spatie/laravel-activitylog                                                                                                                                                                                                                                    `

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 &amp;amp; get back space after clean. ](#content-mysql---rebuild-index--get-back-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 `delete_records_older_than_days` of the config file.

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

```
php artisan activitylog:clean
```

```
//app/Console/Kernel.php

protected function schedule(Schedule $schedule)
{
   $schedule->command('activitylog:clean')->daily();
}
```

If you want to automatically cleanup your `production` system you should append the `--force` option as the command will otherwise ask you to confirm the action. 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 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 command option. This will overwrite the config for this run.

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

MySQL - Rebuild index &amp; get back space after clean.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

After clean, you might experience database table size still allocated more than actual lines in table, execute this line in MySQL to OPTIMIZE / ANALYZE table.

```
OPTIMIZE TABLE activity_log;
```

OR

```
ANALYZE TABLE activity_log;
```

\*this SQL operation will lock write/read of database, use ONLY when server under maintanance mode.
