Hello WordPress nerds, welcome to Mavnty, In this blog post I am talking about how can you remove the default Action Scheduler Task from your WordPress Site.
What is Action Scheduler
Action Scheduler is a powerful tool in WordPress that allows developers to schedule and manage background tasks efficiently. By default, completed tasks are stored in the database for 30 days. However, there might be cases where you wish to remove completed tasks earlier or immediately after they are finished. In this blog post, we will explore how to adjust the retention period for completed tasks using PHP code snippets. By the end of this article, you will have the knowledge to customize the retention period according to your specific needs.
Understanding the Action Scheduler Retention Period
The retention period is the duration for which completed tasks remain in the database. To modify this period, we will use a PHP code snippet that filters the “action_scheduler_retention_period” hook. By default, completed tasks are retained for 30 days, but we can change this to a shorter duration or even remove them immediately.
Customizing the Retention Period
To adjust the retention period, you can use the following code snippet in your WordPress site:
function sa_as_retention_period() {
return DAY_IN_SECONDS; // Change this value to customize the retention period
}
add_filter( 'action_scheduler_retention_period', 'sa_as_retention_period' );
In the above code, we have set the retention period to one day, meaning that completed tasks will be removed automatically after 24 hours. If you wish to use a different time frame, you can replace DAY_IN_SECONDS
with one of the following values:
MINUTE_IN_SECONDS
: Completed tasks will be removed after one minute.HOUR_IN_SECONDS
: Completed tasks will be removed after one hour.DAY_IN_SECONDS
: Completed tasks will be removed after one day.WEEK_IN_SECONDS
: Completed tasks will be removed after one week.
Choose the value that best suits your requirements.
Implementing the Code Snippet
To add the PHP code snippet to your WordPress site, follow these steps:
- Open your WordPress admin panel and navigate to “Appearance” > “Theme Editor.“
- On the right-hand side, find the “Theme Functions” file (usually named functions.php) and click on it.
- Insert the PHP code snippet provided above at the end of the file.
- Save the changes by clicking on the “Update File” button.
Conclusion
Action Scheduler is a valuable tool for managing background tasks in WordPress. By customizing the retention period for completed tasks, you can optimize your database and ensure efficient task management. Use the PHP code snippet provided in this blog post to set the desired retention period based on your specific needs. Whether you want to remove completed tasks after a few minutes, hours, or immediately after completion, Action Scheduler’s flexibility allows you to tailor the retention period accordingly.