# `hdel`

_Delete a file or directory_
```syntax
hdel x     hdel[x]
```

Where `x` is a [file symbol](glossary.md#file-symbol) atom, deletes the file or directory and returns `x`.
```q
q)`:test.txt 0: enlist "hello"
`:test.txt
q)hdel`:test.txt   / delete test.txt in current working directory
`:test.txt
q)hdel`:test.txt   / file no longer exists
'test.txt. OS reports: No such file or directory
  [0]  hdel`:test.txt   / file no longer exists
       ^
```

`hdel` can delete a directory only if it is empty.
```q
q)`:mydir/a/b.txt 0: enlist "ab"
`:mydir/a/b.txt
q)`:mydir/c.txt 0: enlist "cd"
`:mydir/c.txt
q)hdel`:mydir
'mydir. OS reports: Directory not empty
  [0]  hdel`:mydir
       ^
```

To delete a directory and its contents, list its contents recursively with [`.z.s`](dotz.md#zs-self) and delete them in descending order, so that each directory is deleted after its contents:
```q
q)/ dir gets recursive directory listing
q)dir:{$[11h=type d:key x;raze x,.z.s each` sv/:x,/:d;d]}
q)dir`:mydir
`:mydir`:mydir/a`:mydir/a/b.txt`:mydir/c.txt
q)nuke:hdel each desc dir@
q)nuke`:mydir
`:mydir/c.txt`:mydir/a/b.txt`:mydir/a`:mydir
```

For a general visitor pattern with `hdel`:
```q
q)visitNode:{if[11h=type d:key y;.z.s[x]each` sv/:y,/:d;];x y}
q)nuke:visitNode[hdel]
```

> **Warning — Unlike Linux, Windows doesn’t let you overwrite files that are memory-mapped, and it takes a few milliseconds after unmapping before you can.**
>
>
----

[File system](files.md)
