Copy and delete a directory with WordPress functions

The method rmdir()source – :

/**
 * Delete a directory.
 *
 * @since 2.5.0
 *
 * @param string $path Path to directory.
 * @param bool $recursive Optional. Whether to recursively remove files/directories.
 * Default false.
 * @return bool Whether directory is deleted successfully or not.
 */
public function rmdir( $path, $recursive = false ) {
    return false;
}

from the WP_Filesystem_Base class should work for that purpose.

The method rmdir() is part of the classes that extend WP_Filesystem_Base, like WP_Filesystem_Direct and accordingly there is the rmdir() method in that class. Take a look at the the wp-admin/includes directory to inspect the other classes yourself.

So just copy the directory with copy_dir()source – and then remove it with above method.

A bit more general information about the Filesystem API can be found at the codex page.

Leave a Comment