#64553 closed enhancement (wontfix)
Add parameter to delete_expired_transients() for custom transients
| Reported by: | Guido07111975 | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | |
| Component: | Database | Version: | |
| Severity: | normal | Keywords: | has-patch needs-unit-tests changes-requested |
| Cc: | Focuses: |
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)
Change History (15)
#2
@
9 months ago
Thanks! Shouldn't the timeout transient also support the custom name?
$wpdb->esc_like( '_transient_timeout_' ) . '%', $wpdb->esc_like( '_site_transient_timeout_' ) . '%',
#3
@
9 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
@
9 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
@
9 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
@
9 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.
9 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
@
9 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.
@solankisoftware commented on PR #10830:
9 months ago
#9
I’ve restored the @global wpdb documentation and pushed an update.
@solankisoftware commented on PR #10830:
8 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. 👍
#12
@
2 months ago
- Milestone Awaiting Review
- Resolution → wontfix
- Status new → closed
I don't see any benefit to adding this.
The delete_expired_transients is primarily intended for WordPress Core to run daily on the scheduled event of the same name, see code reference.
Limiting the function to deleting a subset of expired transients doesn't make sense, as once the transient is expired it can't be reached. get_transient() will simply discard any data returned from the database query.
As the function isn't really intended to be called by plugins as there's no effect on other plugins by deleting the expired data, I'm going to close this as wontfix.
#13
@
2 months ago
I disagree, because it does not always run daily (or at all). This may cause flooding of the database, when a theme/plugin uses transients to temporary store data.
I created this ticket because of the following forum thread: https://wordpress.org/support/topic/great-simple-contact-form-plugin-but/.
With this parameter available, it could be used to clean up expired plugin/theme specific transients, without touching other ones.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
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.