delete a directory in typescript

To delete a directory in TypeScript, you can use the built-in file system (fs) module from Node.js. Here is an example code snippet that demonstrates how to delete a directory in TypeScript:

index.ts
import * as fs from 'fs';

const directoryPath = '/path/to/directory';

// Check if the directory exists
if (fs.existsSync(directoryPath)) {

  // Delete the directory recursively
  fs.rmdirSync(directoryPath, { recursive: true });

  console.log(`${directoryPath} has been deleted.`);

} else {

  console.log(`${directoryPath} does not exist.`);

}
351 chars
18 lines

The fs.existsSync() method is used to check if the directory exists before attempting to delete it. If the directory exists, the fs.rmdirSync() method is used to delete the directory and its contents recursively. The { recursive: true } option is passed to the fs.rmdirSync() method to ensure that all subdirectories and files are deleted as well.

If the directory does not exist, a message is logged stating that it does not exist.

gistlibby LogSnag