Skip to content

Add automatic cleanup of old core files using WordPress $_old_files list#299

Open
Copilot wants to merge 43 commits intomainfrom
copilot/add-delete-old-core-files
Open

Add automatic cleanup of old core files using WordPress $_old_files list#299
Copilot wants to merge 43 commits intomainfrom
copilot/add-delete-old-core-files

Conversation

Copy link
Contributor

Copilot AI commented Nov 7, 2025

Feature: Add support for deleting old core files using WordPress $_old_files list

Problem

Currently, when checksums for the target WordPress version and locale aren't available, WP-CLI warns users to manually clean up old files. This is inconvenient and error-prone.

Solution

This PR implements automatic cleanup using WordPress core's $_old_files global array from wp-admin/includes/update-core.php.

Cleanup Order

Old files from WordPress core's $_old_files list are always cleaned up first, before fetching and comparing checksums. If checksums are available, the checksum-based diff cleanup follows.

Changes Made

  1. Modified cleanup_extra_files() - cleanup_old_files() now runs unconditionally at the start
  2. cleanup_old_files() - runs unconditionally
  3. Removed cleanup_old_files_not_in_checksums() - now redundant
  4. Added get_old_files_list() - centralizes access to $_old_files array
  5. Added remove_old_files_from_list() - single-pass helper with early exit and path validation
  6. Added remove_directory() - uses RecursiveIteratorIterator/RecursiveDirectoryIterator with CHILD_FIRST ordering
  7. Updated command docblock examples to reflect new cleanup output:
    • Standard update example: No files found that need cleaning upNo old files were removed.
    • Force to 3.1 example: added No old files were removed. and the warning about checksums being unavailable
  8. Updated test for "Update from a ZIP file" to include new cleanup log messages

Testing

  • Two comprehensive Behat test scenarios for new functionality
  • "Update from a ZIP file" scenario updated for new log messages
  • No breaking changes
  • Backward compatible
Original prompt

This section details on the original issue you should resolve

<issue_title>Way to delete old core files after core update</issue_title>
<issue_description>## Feature Request

Describe your use case and the problem you are facing
Currently there is no way to delete old core files if during the update checksums for target WP version and locale aren't available.
Describe the solution you'd like
Command or a flag to delete old core files.
A clear and concise description of what you want to happen.
A way to delete old core files after core update exists.
</issue_description>

<agent_instructions>The list of old files in core can be retrieved by including wp-admin/includes /update-core.php and then accessing the global $_old_files variable</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@schlessera WP-CLI currently diffs the old checksums against the new checksums to decide what left-overs to purge in https://github.com/wp-cli/core-command/blob/1fa06e0cdb5d378585a69d01c94ad2d33278cf28/src/Core_Command.php#L1350-L1393

However, for combinations of locales & versions that we don't have checksums available, this just fails with a message that cleaning up by hand is required.

However, WordPress provides a list of files that are supposed to be deleted and deletes them by default on update: https://github.com/WordPress/wordpress-develop/blob/a216b6d83845dc4cdd01ff517cdf36b72043b6b3/src/wp-admin/includes/update-core.php#L20-L791

We should combine this data with the checksums, to use the checksums to only delete old files when they have not been modified, and to unconditionally delete old files when we don't have checksums to verify for modifications.</comment_new>
<comment_new>@schlessera
@Aaron-Ritter This is indeed still an issue. The latest update for this is that release 5.9 showed that the WordPress update process is broken as well and will be rethought for 6.0. Hopefully, that rethinking can also include WP-CLI-usable logic that just works.</comment_new>


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 5 commits November 7, 2025 17:05
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
…ication

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Copilot AI changed the title [WIP] Add command to delete old core files after update Add automatic cleanup of old core files using WordPress $_old_files list Nov 7, 2025
Copilot AI requested a review from swissspidy November 7, 2025 17:20
@swissspidy swissspidy requested a review from Copilot November 7, 2025 18:06

This comment was marked as resolved.

@codecov
Copy link

codecov bot commented Nov 7, 2025

Codecov Report

❌ Patch coverage is 58.94737% with 39 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/Core_Command.php 58.94% 39 Missing ⚠️

📢 Thoughts on this report? Let us know!

swissspidy and others added 3 commits November 7, 2025 22:18
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copilot AI and others added 5 commits November 7, 2025 21:22
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
@wojsmol
Copy link
Contributor

wojsmol commented Nov 8, 2025

we also need to check that this will not cause a regression of #196

@swissspidy swissspidy added the command:core-update Related to 'core update' command label Nov 8, 2025
@swissspidy

This comment was marked as resolved.

swissspidy and others added 2 commits March 11, 2026 23:29
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…realpath calls

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
… into a single pass

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Copilot AI requested a review from swissspidy March 12, 2026 08:09
@swissspidy swissspidy marked this pull request as ready for review March 12, 2026 11:19
@swissspidy swissspidy requested a review from a team as a code owner March 12, 2026 11:19
@swissspidy

This comment was marked as resolved.

gemini-code-assist[bot]

This comment was marked as resolved.

swissspidy and others added 2 commits March 12, 2026 12:29
Co-authored-by: Pascal Birchler <pascal.birchler@gmail.com>
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

$has_checksums = is_array( $old_checksums ) && is_array( $new_checksums );

if ( ! $has_checksums ) {
WP_CLI::warning( 'Could not retrieve WordPress core checksums; skipping checksum-based cleanup. Files listed in $_old_files were still cleaned up.' );
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new warning when checksums aren’t available drops the specific context that get_core_checksums() returns (e.g. Checksums not available for WordPress <version>/<locale>. or an exception message). This is a user-visible regression in diagnostic quality compared to the previous warnings.

Suggested fix: include the underlying $old_checksums/$new_checksums string(s) in the warning (and ideally indicate whether the "from" or "to" checksums failed) while still noting that $_old_files cleanup was attempted.

Suggested change
WP_CLI::warning( 'Could not retrieve WordPress core checksums; skipping checksum-based cleanup. Files listed in $_old_files were still cleaned up.' );
$details = array();
if ( ! is_array( $old_checksums ) && $old_checksums ) {
$old_message = is_string( $old_checksums ) ? $old_checksums : wp_json_encode( $old_checksums );
$details[] = sprintf(
'"from" version (%s) checksums: %s',
$version_from,
$old_message
);
}
if ( ! is_array( $new_checksums ) && $new_checksums ) {
$new_message = is_string( $new_checksums ) ? $new_checksums : wp_json_encode( $new_checksums );
$details[] = sprintf(
'"to" version (%s) checksums: %s',
$version_to,
$new_message
);
}
$details_message = $details ? ' Details: ' . implode( ' | ', $details ) : '';
WP_CLI::warning(
'Could not retrieve WordPress core checksums; skipping checksum-based cleanup. Files listed in $_old_files were still cleaned up.' . $details_message
);

Copilot uses AI. Check for mistakes.
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Copilot AI requested a review from swissspidy March 13, 2026 09:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

command:core-update Related to 'core update' command

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Way to delete old core files after core update

4 participants