Martin Braun
Martin Braun

Reputation: 12589

How to remove all files from directory without removing the directory in Deno?

How to remove all files from a directory without removing the directory itself using Deno?

Upvotes: 0

Views: 462

Answers (1)

Martin Braun
Martin Braun

Reputation: 12589

This can be done very easily with the fs module:

import * as fs from "https://deno.land/std/fs/mod.ts";
await fs.emptyDir("path/to/dir");

It's also possible to iterate the files for some finer control without using the fs module:

import * as path from "https://deno.land/std/path/mod.ts";

const dirPath = path.join("path", "to", "dir"); // “path/to/dir”
for await(const dirEntry of Deno.readDir(dirPath)) {
    await Deno.remove(path.join(dirPath, dirEntry.name), { recursive: true });
}

Requires --allow-read and --allow-write permissions, in any case.

Upvotes: 1

Related Questions