Title: WP_Upgrader::clear_destination
Published: August 18, 2015
Last modified: May 20, 2026

---

# WP_Upgrader::clear_destination( string $remote_destination ): true|󠀁[WP_Error](https://developer.wordpress.org/reference/classes/wp_error/)󠁿

## In this article

 * [Parameters](https://developer.wordpress.org/reference/classes/wp_upgrader/clear_destination/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/classes/wp_upgrader/clear_destination/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/classes/wp_upgrader/clear_destination/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/classes/wp_upgrader/clear_destination/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/classes/wp_upgrader/clear_destination/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/classes/wp_upgrader/clear_destination/?output_format=md#wp--skip-link--target)

Clears the directory where this item is going to be installed into.

## 󠀁[Parameters](https://developer.wordpress.org/reference/classes/wp_upgrader/clear_destination/?output_format=md#parameters)󠁿

 `$remote_destination`stringrequired

The location on the remote filesystem to be cleared.

## 󠀁[Return](https://developer.wordpress.org/reference/classes/wp_upgrader/clear_destination/?output_format=md#return)󠁿

 true|[WP_Error](https://developer.wordpress.org/reference/classes/wp_error/) True
upon success, [WP_Error](https://developer.wordpress.org/reference/classes/wp_error/)
on failure.

## 󠀁[Source](https://developer.wordpress.org/reference/classes/wp_upgrader/clear_destination/?output_format=md#source)󠁿

    ```php
    public function clear_destination( $remote_destination ) {
    	global $wp_filesystem;

    	$files = $wp_filesystem->dirlist( $remote_destination, true, true );

    	// False indicates that the $remote_destination doesn't exist.
    	if ( false === $files ) {
    		return true;
    	}

    	// Flatten the file list to iterate over.
    	$files = $this->flatten_dirlist( $files );

    	// Check all files are writable before attempting to clear the destination.
    	$unwritable_files = array();

    	// Check writability.
    	foreach ( $files as $filename => $file_details ) {
    		if ( ! $wp_filesystem->is_writable( $remote_destination . $filename ) ) {
    			// Attempt to alter permissions to allow writes and try again.
    			$wp_filesystem->chmod( $remote_destination . $filename, ( 'd' === $file_details['type'] ? FS_CHMOD_DIR : FS_CHMOD_FILE ) );
    			if ( ! $wp_filesystem->is_writable( $remote_destination . $filename ) ) {
    				$unwritable_files[] = $filename;
    			}
    		}
    	}

    	if ( ! empty( $unwritable_files ) ) {
    		return new WP_Error( 'files_not_writable', $this->strings['files_not_writable'], implode( ', ', $unwritable_files ) );
    	}

    	if ( ! $wp_filesystem->delete( $remote_destination, true ) ) {
    		return new WP_Error( 'remove_old_failed', $this->strings['remove_old_failed'] );
    	}

    	return true;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-admin/includes/class-wp-upgrader.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.0/src/wp-admin/includes/class-wp-upgrader.php#L441)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.0/src/wp-admin/includes/class-wp-upgrader.php#L441-L477)

## 󠀁[Related](https://developer.wordpress.org/reference/classes/wp_upgrader/clear_destination/?output_format=md#related)󠁿

| Uses | Description | 
| [WP_Upgrader::flatten_dirlist()](https://developer.wordpress.org/reference/classes/wp_upgrader/flatten_dirlist/)`wp-admin/includes/class-wp-upgrader.php` |

Flattens the results of [WP_Filesystem_Base::dirlist()](https://developer.wordpress.org/reference/classes/wp_filesystem_base/dirlist/) for iterating over.

  | 
| [WP_Error::__construct()](https://developer.wordpress.org/reference/classes/wp_error/__construct/)`wp-includes/class-wp-error.php` |

Initializes the error.

  |

| Used by | Description | 
| [WP_Upgrader::install_package()](https://developer.wordpress.org/reference/classes/wp_upgrader/install_package/)`wp-admin/includes/class-wp-upgrader.php` |

Install a package.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wp_upgrader/clear_destination/?output_format=md#changelog)󠁿

| Version | Description | 
| [4.3.0](https://developer.wordpress.org/reference/since/4.3.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_upgrader%2Fclear_destination%2F)
before being able to contribute a note or feedback.