<!--
  canonical-url: https://www.avonture.be/blog/ftp-erase-files
  published:     2024-05-25
  This is a static plain-Markdown mirror generated at build time.
  Visit the canonical URL above for the fully rendered page, with images and interactive components.
-->

# FTP - Remove files and folders at light speed

> Stop the slow process of deleting files via FTP! Learn how to remove entire websites and folders at lightning speed using a simple, self-deleting PHP script—even without SSH access.

---
![FTP - Remove files and folders at light speed](/img/v2/winscp.webp)

**TL;DR**

This article shares a small `erase.php` script you can upload via FTP and run once from the browser to instantly wipe an entire website folder server-side, avoiding the painfully slow file-by-file deletion through an FTP client — with a strong warning that it deletes everything immediately, with no confirmation or backup.

Have you ever deleted a website from your FTP client? It's easy, just select the folder containing the website and press the `delete` key.

Easy, simple and ... so slow. It takes ages to delete files and folders. Several dozen minutes! Ouch.

If you've [an SSH connection to your web server](/blog/connect-using-ssh-to-your-hosting-server) and if you're comfortable with it; you can `rm -rf` the folder and it'll be done in seconds.

But what if you don't have SSH or fear to make errors?

There is an alternative, the `erase.php` script.

Open your FTP client, open your website folder (like `/var/www/html/old_site`); the one you wish to remove and create a new file called f.i. `erase.php` then copy/paste the code below in it.

**erase.php**

```php
<?php

$script_name = basename(__FILE__);
$directory = dirname(__FILE__);

echo '<h1>Deleting all files and subfolders in '.$directory.'</h1>';

$it = new RecursiveDirectoryIterator($directory);

foreach (new RecursiveIteratorIterator($it, RecursiveIteratorIterator::CHILD_FIRST) as $file) {
  if ( ($file->isDir()) && (!in_array($file->getFilename(), array('.', '..'))) ){
    echo '<h3>Deleting folder '.$file->getPathname().'</h3>';
    rmdir($file->getPathname());
  } else {
    if ( ($file->isFile()) && ($file->getPathname()!=__FILE__) ) {
      echo '<p>Deleting file '.$file->getPathname().'</p>';
      unlink ($file->getPathname());
    }
  }
}

echo '<h4 style="color:green;">Deletion complete.  Only the '.$script_name.' file remains in the folder.</h4>';

unlink(__FILE__);
?>
```

Start your browser, navigate to your website (`http://your_old_site.com`) and add `/erase.php` at the end to run the script.

> **Make sure this is what you want:**
>
> Be really sure this is what you want

> **Be extremely sure this is what you want:**
>
> The script will then start immediately and will remove everything without asking confirmation nor backup. Just killing files and folders.
>
> At the end of the process (i.e. in just a few seconds), the folder will be completely empty.
>
> *And, exactly like [the QuickScan scanner](/blog/aesecure-quickscan), remember to delete `erase.php` from your server as soon as you're done: a script able to wipe a folder has no business staying reachable from the Internet.*
