Make WordPress Core

Opened 5 months ago

Last modified 4 months ago

#64553 new enhancement

Add parameter to delete_expired_transients() for custom transients

Reported by: guido07111975's profile Guido07111975 Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Database Keywords: has-patch needs-unit-tests changes-requested
Focuses: Cc:

Description

I hereby suggest adding an extra parameter to the delete_expired_transients() function, so dev is able to only force delete its own (custom) expired transients. By adding the transient name or its unique prefix to the function, like:

delete_expired_transients( $force_db = false, $name = false ) {

It seems to be something that can be added easily in file wp-includes/option.php.

IMO a plugin/theme should be able to delete its own data, but it should not delete data (expired transients) that are not related to the plugin/theme.

Attachments (2)

64553-option-php.patch (12.1 KB) - added by solankisoftware 5 months ago.
option-docblock-cleanup.patch (3.0 KB) - added by solankisoftware 5 months ago.
New updated patch

Download all attachments as: .zip

Change History (13)

#1 @solankisoftware
5 months ago

  • Keywords has-patch added

This patch focuses on extending delete_expired_transients() to optionally target expired transients matching a given prefix. I’ll update the patch to remove unrelated docblock changes and keep it fully scoped to this ticket. Feedback welcome on approach and API design.

#2 @Guido07111975
5 months ago

Thanks! Shouldn't the timeout transient also support the custom name?

$wpdb->esc_like( '_transient_timeout_' ) . '%',
$wpdb->esc_like( '_site_transient_timeout_' ) . '%',

@solankisoftware
5 months ago

New updated patch

#3 @solankisoftware
5 months ago

Yes — the updated patch accounts for that.
Timeout transients are deleted via their relationship to the filtered value transients (_transient_{$name} / _site_transient_{$name}), so providing a custom name or prefix implicitly applies to the corresponding timeout entries as well.

I’ve updated the patch accordingly and removed unrelated changes to keep the scope focused.

#4 @soyebsalar01
4 months ago

Tested this: delete expired_transients currently remove all expired transients. There is no way to scope cleanup to plugin's own transients, which makes targeted cleanup difficult. A scoped parameter would address this well.

#5 @solankisoftware
4 months ago

Thanks for testing and confirming the issue.

Yes, the current behavior removes all expired transients, which makes it difficult for plugins/themes to safely clean up only their own data.

The proposed scoped parameter allows targeted cleanup by transient name or prefix, keeping the default behavior unchanged while enabling safer, plugin-specific cleanup when needed.

#6 @westonruter
4 months ago

  • Keywords needs-unit-tests added

The $name parameter should be changed to something like $prefix. The docs should also make it clear that this only applies when not using an external object cache or if $force_db is enabled.

Also, please open a pull request instead of attaching patches. This is easier for review and it runs automated tests.

Speaking of which, this will need unit tests.

This ticket was mentioned in PR #10830 on WordPress/wordpress-develop by @solankisoftware.


4 months ago
#7

Adds an optional $prefix parameter to delete_expired_transients() to allow
scoped cleanup of expired transients when not using an external object cache
or when $force_db is enabled.

This enables plugins to safely clean up only their own expired transients.

#8 @solankisoftware
4 months ago

PR opened: https://github.com/WordPress/wordpress-develop/pull/10830

The PR updates delete_expired_transients() to support scoped cleanup
via a $prefix parameter and clarifies behavior with external object cache.

Last edited 4 months ago by solankisoftware (previous) (diff)

@solankisoftware commented on PR #10830:


4 months ago
#9

I’ve restored the @global wpdb documentation and pushed an update.

#10 @mukesh27
4 months ago

  • Keywords changes-requested added
  • Type changed from feature request to enhancement
  • Version 6.9 deleted

@solankisoftware commented on PR #10830:


4 months ago
#11

Thanks for the review. I’ve updated the PR to allow deleting expired transients by prefix only, ensured it runs strictly on expired entries, and aligned everything with core standards.
All feedback is addressed and it’s ready for review. 👍

Note: See TracTickets for help on using tickets.