Delete a directory and all its contents using Javascript, Typescript, or Php

It is well known that when you try to programmatically delete a directory, the operation will fail if it is not empty. That is not a pretty useful feature when you consider that directories are normally used to store things. So when performing this operation with a programming language, the common practice is to implement a recursive loop to check all the directory contents, deleting those which are files and performing the same recursive loop again for those which are directories. It may become a bit tedious to code this, so when you need to delete a directory recursively making sure everything inside gets removed, you can use the TurboDepot FilesManager class which has a method to do exactly this. Let's see how to do it for several programming languages:

Delete a directory and all its contents with PHP

Download the latest TurboCommons and TurboDepot phar files, place them on your project as dependencies and run the following code:

require 'path/to/your/dependencies/folder/turbocommons-php-X.X.X.phar';
require 'path/to/your/dependencies/folder/turbodepot-php-X.X.X.phar';

use org\turbodepot\src\main\php\managers\FilesManager;

$filesManager = new FilesManager();
$filesManager->deleteDirectory('path/to/some/directory/to/delete');
$filesManager->deleteDirectory('path/to/some/directory/to/delete2', false);

The first call to deleteDirectory() will perform all the operations to clean the directory contents and delete the directory itself. The second one will delete all the directory contents but won't delete the directory itself, leaving it basically empty.

Delete a directory and all its contents with Javascript (nodejs)

Install the turbodepot dependency by executing the following command at the root of your project:

npm install turbodepot-node

And then run the following code:

const { FilesManager } = require('turbodepot-node');
let filesManager = new FilesManager();
filesManager.deleteDirectory('path/to/some/directory/to/delete');
filesManager.deleteDirectory('path/to/some/directory/to/delete2', false);

The first call to deleteDirectory() will perform all the operations to clean the directory contents and delete the directory itself. The second one will delete all the directory contents but won't delete the directory itself, leaving it basically empty.

Delete a directory and all its contents with Typescript (TS running on nodejs)

Install the turbodepot dependency by executing the following command at the root of your project:

npm install turbodepot-node

And then run the following code:

import { FilesManager } from 'turbodepot-node';
let filesManager = new FilesManager();
filesManager.deleteDirectory('path/to/some/directory/to/delete');
filesManager.deleteDirectory('path/to/some/directory/to/delete2', false);

The first call to deleteDirectory() will perform all the operations to clean the directory contents and delete the directory itself. The second one will delete all the directory contents but won't delete the directory itself, leaving it basically empty.