Removing an operating system file from a PL/SQL block.
GOAL

Removing an operating system file from a OS directory using the UTL_FILE package.

RELATED

HOW TO WRITE TO A FILE FROM PL/SQL BLOCK
HOW TO READ A FILE FROM PL/SQL BLOCK
HOW TO COPY AN OPERATING SYSTEM FILE FROM PL/SQL BLOCK

DEMO

The FREMOVE procedure of the UTL_FILE package deletes a disk file, assuming that you have sufficient privileges.

UTL_FILE.FREMOVE (
   location IN VARCHAR2,
   filename IN VARCHAR2);

location - Directory location of the file, a DIRECTORY_NAME from ALL_DIRECTORIES
filename - Name of the file to be deleted
NOTE

The file to be deleted was created in HOW TO COPY AN OPERATING SYSTEM FILE FROM PL/SQL BLOCK

## The file

ls -lh /tmp/dir2/myfile.cloned.txt --time-style='+'
-rw-r--r--. 1 oracle dba 32  /tmp/dir2/myfile.cloned.txt
-- Remove OS file

SET SERVEROUTPUT ON

BEGIN
   UTL_FILE.FREMOVE('DIR2', 'myfile.cloned.txt');
END;
/

Confirm that file does not exist anymore

ls -lh /tmp/dir2/myfile.cloned.txt --time-style='+'

ls: cannot access /tmp/dir2/myfile.cloned.txt: No such file or directory

That's it. The file deleted.

Written At
04 JAN 202221:00
OEL Release
7.9 x64
Database Release
12.1.0.1.0 - 19.13.0.0.0