WP-Cron can quietly create a problem when a plugin schedules the same event during every WordPress load. Instead of a single recurring task, dozens or hundreds of identical events accumulate in the queue.
The consequences are visible beyond the cron event list. Imports may run in parallel, records may be processed repeatedly, and the site and server may receive unnecessary load. Below is a practical way to find duplicates, fix event scheduling, and prevent long-running tasks from running simultaneously.
Why Duplicate WP-Cron Events Appear
WP-Cron stores scheduled events in an internal WordPress structure. When an event is due, WordPress runs the callback associated with the corresponding hook. The problem occurs when code calls wp_schedule_event() on every request, during plugin initialization, or whenever a particular function runs.
This type of code does not check whether the event already exists. Each new request adds another record with the same hook. Scheduling at the init hook or another hook that runs during every site request is especially dangerous.
A recurring event should usually be created only once: during plugin activation, through a separate setup command, or during controlled initialization. Before scheduling it, check the next event that has already been registered.
- Do not call wp_schedule_event() unconditionally on every WordPress load.
- Check the exact hook that later handles the callback.
- Treat duplicate event records and parallel task execution as separate problems.
if ( ! wp_next_scheduled( 'my_sync_hook' ) ) {
wp_schedule_event( time(), 'hourly', 'my_sync_hook' );
}
How to Schedule a Recurring Event Correctly
The wp_next_scheduled() check returns the timestamp of the next scheduled run for the specified hook, or false if no event is found. If no value is returned, you can register the recurring event.
In a plugin, it is better to perform this step in the activation hook and cancel your own event during deactivation. This makes the scheduling lifecycle predictable: activation creates the record, while deactivation removes it.
There is usually no reason to call wp_clear_scheduled_hook() on every request. It may remove the scheduled event, after which the code creates it again. This results in unstable scheduling and makes diagnosis more difficult.
- Use a stable, unique hook for each task.
- If you change the interval, cancel the old event first and then schedule the new one.
- After changing the code, check that old hooks from the previous name have not remained.
register_activation_hook( __FILE__, function () {
if ( ! wp_next_scheduled( 'my_sync_hook' ) ) {
wp_schedule_event( time(), 'hourly', 'my_sync_hook' );
}
} );
register_deactivation_hook( __FILE__, function () {
wp_clear_scheduled_hook( 'my_sync_hook' );
} );
Diagnosing the Queue with WP-CLI
WP-CLI lets you inspect cron events without viewing WordPress's internal records. Run the command from the site root or in an environment where the wp command is available:
In the output, check the hook name, the next run time, and the schedule. The same hook name repeated many times with different timestamps indicates accumulated duplicates. However, multiple events may be intentional if your code deliberately uses different arguments or one-time runs.
- Compare the number of identical hooks before and after fixing the code.
- Check next_run_gmt: past or very closely spaced runs may indicate that cron is falling behind.
- Inspect resource-intensive imports, synchronizations, cleanup tasks, and email delivery separately.
wp cron event list --fields=hook,next_run_gmt,schedule
How to Remove Extra Events Safely
Before cleaning the queue, make sure the hook belongs to your plugin or function. Removing a system event or an event owned by another component could stop an important process. First save the list output, or at least record the hook name and the number of its instances.
Use WP-CLI to remove a specific event. If you need to remove all scheduled instances of one hook, check the available parameters in your WP-CLI version and use the mode that deletes all matching events. Then fix the code; otherwise, duplicates will reappear during subsequent requests.
Cleaning the queue does not run the callback or delete data that has already been written to the database. However, you may need to run the task again after cleanup if the deleted event was responsible for a critical synchronization.
- For a single record, specify the hook and, if needed, the timestamp.
- Use the --all option for bulk removal only after verifying the hook name.
- Run wp cron event list again after cleanup and confirm that the event is created exactly once.
wp cron event delete my_sync_hook
# Variant for deleting all events with this hook,
# if supported by the installed WP-CLI version:
wp cron event delete my_sync_hook --all
Why Duplicate Checks Are Not Enough
Even one correctly scheduled recurring event may run again before the previous process finishes. This can happen when an import takes longer than the configured interval or when multiple cron requests are processed at the same time.
The wp_next_scheduled() check prevents duplicate records from being added to the queue, but it does not lock a process. It does not indicate that the callback is already running. Long-running or sensitive repeated operations need a mutex—a short-lived lock that allows only one instance of the task to run.
The best option for multiple server processes is a persistent object cache with the atomic wp_cache_add() operation. If a persistent object cache is not available, you can use a transient, but it may not provide the same reliable behavior during concurrent requests.
- Set the lock before the first resource-intensive operation.
- If the lock already exists, the callback should exit without starting duplicate work.
- A TTL is mandatory: a stalled process must not block the task forever.
- The TTL should exceed the task's usual duration without being unnecessarily long.
add_action( 'my_sync_hook', function () {
$lock_key = 'my_sync_lock';
$lock_group = 'my_plugin';
$lock_ttl = 15 * MINUTE_IN_SECONDS;
if ( ! wp_cache_add( $lock_key, time(), $lock_group, $lock_ttl ) ) {
return; // Another process is already running the synchronization.
}
try {
// Run the resource-intensive synchronization.
} finally {
wp_cache_delete( $lock_key, $lock_group );
}
} );
Mutex Details and Common Mistakes
The lock key must be specific to the task it protects. If one general lock is used for every cron operation in a plugin, a short import may accidentally block an independent cleanup task or another type of data update.
Do not delete the lock only at the end of the successful execution path. An error, exception, or early termination may leave it in the cache. A try/finally construct helps perform cleanup after an error, while the TTL remains a fallback safeguard.
Remember that WordPress's standard object cache usually exists only for the current request when no persistent backend is configured. In that environment, wp_cache_add() does not provide a lock across requests. For real protection, verify that persistent caching is enabled or design a separate locking mechanism.
A lock does not replace idempotency. The handler should recheck records, statuses, or external identifiers so that a retry after a failure does not create duplicate data.
- Never set a lock without a TTL.
- Do not treat a transient as an unconditionally atomic mutex.
- Do not assume that successfully acquiring a lock proves the previous process completed correctly.
- Add logs for task start, lock-based skips, completion, and errors.
Post-Fix Checklist
After changing the plugin, verify the issue in several stages. First remove old duplicates, then wait for or manually run one event in a test environment. After it runs, inspect the queue and logs again.
Check not only the number of cron records but also the callback's behavior: whether it runs in parallel, whether the lock is released correctly after an error, and whether the same items are processed repeatedly.
- The WP-Cron list contains one expected recurring event.
- Reloading the site does not create new duplicates.
- A parallel run is skipped because of the lock.
- The lock is removed after normal completion.
- The lock disappears after the TTL following an abnormal termination.
- A retry does not create duplicate records or duplicate payments.
Frequently asked questions
Is wp_next_scheduled() enough to protect a task?
No. This function prevents an unnecessary scheduled event from being created, but it does not block a callback that is already running. Long-running tasks need a separate mutex with a TTL.
Why can one cron event run in parallel?
WP-Cron does not guarantee that the previous callback has finished before the next run begins. If processing takes longer than the interval or multiple cron requests arrive at once, a lock check is required before starting the task.
Can I simply delete all events with WP-CLI?
You can if you are certain that the hook belongs to the intended task. First inspect the event list, then fix the scheduling code after deletion; otherwise, duplicates will be created again.
Is a transient suitable for a mutex?
A transient can be a simple option for basic locking, but its behavior depends on the storage system and request concurrency. For a more reliable mutex, use a persistent object cache with atomic key addition.
What should I do if the lock remains after a failure?
The lock should have a TTL that automatically releases it after a stalled process. Also remove the key in finally after the callback finishes normally or with an error.
Conclusion
Reliable WP-Cron starts with correct scheduling: create a recurring event only after checking for an existing one, and remove old records in a controlled way. WP-CLI makes it easy to identify accumulated duplicates and verify the result after fixing the code.
For tasks that may run longer than their interval, this is not enough. Add a mutex with a TTL, guaranteed lock cleanup, and protection against repeated data processing. This combination reduces server load and makes cron processes more predictable.