# Keep Completed Background Tasks for 7 Days

> Shortens how long WooCommerce's Action Scheduler keeps finished tasks from 31 days to 7, which keeps its database tables small on busy stores.

- Type: PHP
- Runs: Run everywhere
- Category: [WooCommerce](https://wpide.com/library/category/woocommerce/)
- Requires: WooCommerce
- Tags: woocommerce, performance, database, action-scheduler
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/woo-action-scheduler-retention/

## Code

```php
$wpide_as_retention_days = 7; // Days to keep completed actions.

add_filter( 'action_scheduler_retention_period', function () use ( $wpide_as_retention_days ) {
    return $wpide_as_retention_days * DAY_IN_SECONDS;
} );
```

## Notes

Only completed and cancelled actions are cleared, gradually by Action Scheduler itself, and pending tasks are never touched.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Keep Completed Background Tasks for 7 Days" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
