Skip to content

DELETE Statement

The DELETE statement logically removes a record from a relative, or indexed file.

General Format:

Format 1

       DELETE file-1 RECORD 
              [ INVALID KEY statement-1 ] 
              [ NOT INVALID KEY statement-2 ]
              [ END-DELETE ]

Syntax:

file-n is a file described in the File Section with an FD.

General Rules:

  1. file-1 must be described with ORGANIZATION IS RELATIVE or ORGANIZATION IS INDEXED.
  2. file-1 must be OPEN I-O when the DELETE file-1 RECORD statement is executed.
  3. For files declared with ACCESS IS SEQUENTIAL:
    • The INVALID KEY/NOT INVALID KEY clauses cannot be used.
    • The previous I/O statement must be a successful READ. That record is DELETE’d from the file.
  4. For files declared with ACCESS IS DYNAMIC or ACCESS IS RANDOM:
    • The INVALID KEY/NOT INVALID KEY clauses can be used
  5. For relative files, the RELATIVE KEY is used to identify the record to be DELETE’d. If the RELATIVE KEY data item does not identify a record, then the INVALID KEY condition is raised.
  6. For indexed files, the RECORD KEY is used to identify the record to be DELETE’d. If the RECORD KEY data item does not identify a record, then the INVALID KEY condition is raised.
  7. The INVALID KEY condition can be handled programmatically using the INVALID KEY clause. Statement-1 executes when the INVALID KEY condition is detected. Statement-2 executes when the INVALID KEY condition is NOT detected.
  8. In the absence of the INVALID KEY clause, the INVALID KEY condition causes a File I/O error condition, which can be handled in DECLARATIVES. If it is not handled in DECLARATIVES, or if there are no DECLARATIVES, then the INVALID KEY condition updates the file status variable and aborts the program.
  9. If successful, the DELETE file-1 RECORD statement removes the identified record from the file.
  10. The DELETE file-1 RECORD statement updates the FILE-STATUS variable.

Format 2

DELETE FILE <filename> erases the file and the optional index.

       DELETE FILE file-1 
              [ END-DELETE ]

Syntax:

file-n is a file described in the File Section with an FD.

General Rules:

When executing a DELETE FILE file-1 statement, file-1 must be CLOSE'd.

Code Sample:

       IDENTIFICATION DIVISION. 
       PROGRAM-ID. DELETE-1. 
       ENVIRONMENT DIVISION. 
       INPUT-OUTPUT SECTION. 
       FILE-CONTROL. 
              SELECT RESWORDS ASSIGN TO "RESWORDS" 
              ORGANIZATION IS INDEXED 
              ACCESS IS DYNAMIC 
              RECORD KEY IS RESERVED-WORD 
              FILE STATUS IS RESWORDS-STAT. 

       DATA DIVISION. 
       FILE SECTION. 
       FD RESWORDS. 
       01 RESWORDS-RECORD. 
              03 RESERVED-WORD     PIC X(30). 

       WORKING-STORAGE SECTION. 
       77 RESWORDS-STAT PIC XX. 
              88 END-OF-RESWORDS VALUE "10". 
       77 DUMMY                    PIC X. 

       PROCEDURE DIVISION. 
       MAIN. 
              OPEN OUTPUT RESWORDS. 
              MOVE "ACCEPT" TO RESERVED-WORD. 
              WRITE RESWORDS-RECORD. 
              MOVE "ADD" TO RESERVED-WORD. 
              WRITE RESWORDS-RECORD. 
              CLOSE RESWORDS. 

              OPEN I-O RESWORDS. 
              MOVE "ACCEPT" TO RESERVED-WORD. 
              READ RESWORDS. 

      * DELETE FILE-NAME RECORD 
      *       [ INVALID KEY STATEMENT-1 ] 
      *       [ NOT INVALID KEY STATEMENT-2 ] 
      *       [ END-DELETE ] 

              DELETE RESWORDS RECORD. 
              IF RESWORDS-STAT = "00" 
                     DISPLAY"RECORD DELETED!"LINE 5 COL 10 
              ELSE 
                     DISPLAY "ERROR! FILE STATUS: " LINE 7 COL 10 
                     DISPLAY RESWORDS-STAT LINE 7 COL 31 
              END-IF. 

              DISPLAY "DELETE-1 FINISHED!" LINE 10 COL 10. 
              ACCEPT DUMMY LINE 10 COL 30. 

              STOP RUN. 
Back to top