PHP Delete All Files and Folders Except One with a Specific Name

Managing files and directories in PHP can be incredibly useful for cleaning up temp directories or resetting folders. In this tutorial, you’ll learn how to delete everything inside a directory except for one specific folder using PHP.

Use Case

Let’s say your PHP application generates files and folders regularly, but you want to clean them up periodically — except for one important folder (e.g., a folder named keep_folder). This script makes it easy to do that.

The PHP Script

Here is a simple PHP script that scans a directory and deletes all files and folders except the one you want to preserve.

<?php
$directory = __DIR__; // example:"D:/xampp/htdocs/web/folderdelete" or specify path like '/var/www/html/folder' 
$except = 'keep_folder'; // folder you want to keep

$items = scandir($directory);

foreach ($items as $item) {
    if ($item === '.' || $item === '..' || $item === $except) {
        continue;
    }

    $path = $directory . DIRECTORY_SEPARATOR . $item;

    if (is_dir($path)) {
        deleteFolder($path);
    } else {
        unlink($path); // delete file
    }
}

// Recursive folder delete function
function deleteFolder($folder) {
    $files = array_diff(scandir($folder), ['.', '..']);
    foreach ($files as $file) {
        $filePath = "$folder/$file";
        is_dir($filePath) ? deleteFolder($filePath) : unlink($filePath);
    }
    return rmdir($folder);
}

echo "All items except '$except' have been deleted.";


Code Explanation:

LineWhat It Does
$directory = __DIR__;Sets the directory (you can change it).
$except = 'keep_folder';Folder name to keep safe.
scandir($directory);Lists all items in the directory.
unlink($path);Deletes a file.
deleteFolder($path);Recursively deletes a folder and its contents.


Safety Tips

  • Back up your data before running this script.
  • Always test in a sandbox directory first.
  • Confirm permissions: The PHP process must have write/delete access.

Suggested Images for Article

Feature Image:

A stylized image showing a folder structure with one folder untouched and others being deleted.

Suggested title: “Selective Folder Cleanup in PHP”

Example Prompt for AI image generator:

A computer folder interface with multiple folders being deleted except one labeled "keep_folder", clean modern style

Code Diagram Image:

Visualize how the script checks each file/folder and keeps one.

  • Flowchart with: scandir() → loop → if condition → delete or skip

Before & After Screenshot:

A screenshot of a directory:

  • Before: Shows multiple folders and files.
  • After: Shows only the keep_folder.

Terminal Output Image:

A terminal or browser screenshot showing:

All items except 'keep_folder' have been deleted.


Final Thoughts

This script can be a powerful tool in server automation, cron jobs, or when managing temp folders in WordPress, Laravel, or custom PHP apps.

You can also modify this script to only delete files, or keep multiple folders, depending on your need.


Previous Post Next Post