INT 21h AH=41h removes a file from its directory. The name is ASCIIZ and may include a drive and a path. It must name one file: this function takes no wildcards, so to delete *.TMP a program finds each match with INT 21h AH=4Eh / 4Fh and deletes it by name.

AXMeaning
02hfile not found
03hpath not found: a directory in the name does not exist
05haccess denied: the file is read-only, the name is a directory (those go with INT 21h AH=3Ah) or a device
prints two lines: "first delete: ok" and "second delete: CF = 1, there is nothing left to delete"
MOV AH, 3Ch ; DOS: create TEST.TXT
XOR CX, CX ; CX = 0: normal attributes
MOV DX, OFFSET FNAME
INT 21h
CALL CHECK ; stop if CF = 1
MOV BX, AX ; BX = the handle
MOV AH, 3Eh ; close it first
INT 21h
MOV DX, OFFSET M1
MOV AH, 09h ; DOS: print the string at DS:DX
INT 21h
MOV AH, 41h ; DOS: delete TEST.TXT
MOV DX, OFFSET FNAME
INT 21h
CALL REPORT
MOV DX, OFFSET M2
MOV AH, 09h ; DOS: print the string at DS:DX
INT 21h
MOV AH, 41h ; the same name again
MOV DX, OFFSET FNAME
INT 21h
MOV DX, OFFSET GONE1 ; CF = 1: nothing by that name
JC SHOW1
MOV DX, OFFSET ODD1
SHOW1:
MOV AH, 09h
INT 21h
FNAME DB "TEST.TXT", 0
M1 DB "first delete: $"
M2 DB "second delete: $"
GONE1 DB "CF = 1, there is nothing left to delete", 13, 10, "$"
ODD1 DB "CF = 0?", 13, 10, "$"
CRLF DB 13, 10, "$"
CHECK PROC NEAR ; CF = 1: print the error code in AL and exit
JNC CHECK1
PUSH AX
MOV DX, OFFSET FAILMSG
MOV AH, 09h
INT 21h
POP AX
CALL PHEXB
MOV AX, 4C01h ; exit with code 1
INT 21h
CHECK1:
RET
CHECK ENDP
FAILMSG DB "failed: error $"
REPORT PROC NEAR ; CF = 0: "ok"; CF = 1: "error" and AL
JC REPORT1
MOV DX, OFFSET OKMSG
MOV AH, 09h
INT 21h
RET
REPORT1:
PUSH AX
MOV DX, OFFSET ERRMSG
MOV AH, 09h
INT 21h
POP AX
CALL PHEXB ; the error code
MOV DX, OFFSET CRLF
MOV AH, 09h
INT 21h
RET
REPORT ENDP
OKMSG DB "ok", 13, 10, "$"
ERRMSG DB "error $"
PHEXB PROC NEAR ; print AL as two hex digits
PUSH CX
PUSH DX
MOV DH, AL ; keep the byte
MOV CL, 4
SHR AL, CL ; the high digit first
MOV CX, 2
PHEXB1:
AND AL, 0Fh
ADD AL, "0"
CMP AL, "9"
JBE PHEXB2
ADD AL, 7 ; 10..15 -> "A".."F"
PHEXB2:
MOV DL, AL
MOV AH, 02h ; DOS: print DL
INT 21h
MOV AL, DH ; then the low digit
LOOP PHEXB1
POP DX
POP CX
RET
PHEXB ENDP

On MS-DOS the second delete returns AX = 02h, file not found. DOSBox-X, on a host folder, can leave an earlier access-denied code in AX instead, which is why the example tests only CF.

What happens on a FAT disk: DOS marks the directory entry free by overwriting the first character of the name with E5h, and frees the file's clusters in the FAT. The data stays on the disk until something reuses those clusters, which is how UNDELETE can bring a file back if you are quick.

Pitfalls

Close a file before deleting it. Without SHARE, MS-DOS deletes an open file without complaint, and later writes through the stale handle land in clusters that now belong to nobody, or to another file. DOSBox-X, on a host folder, closes the program's handles to the file and then deletes it. A read-only file must have its attribute cleared with INT 21h AH=43h first.