FILE DELETE Statement


  • DELETE statement removes a record from an indexed or relative file.
  • After it is successfully executed, the record is removed from the file and can no longer be available.

Points to note -

  • DELETE does not apply to sequential files, as record deletion is impossible.
  • The file must open in I-O mode to execute the DELETE statement.
  • After the successful deletion, the space occupied by the record is marked as available but not immediately used.
  • The record that will be deleted should read first in SEQUENTIAL accessing mode.
  • We should provide RECORD KEY (for indexed files) or RELATIVE KEY (for relative files) for the DELETE statement for DYNAMIC access mode.

Syntax -

DELETE logical-file-name 
           [INVALID KEY statements-set1]
       [NOT INVALID KEY statements-set2]
[END-DELETE].
Note! All statements coded in [ ] are optional.

Parameters -

  • logical-file-name - Specifies the file from which the record is to be deleted.
  • END-DELETE - Marks the end of the DELETE statement. END-DELETE is not required when DELETE statement ended with period.

Error Handling -

  • INVALID KEY - This phrase specifies the action to be taken if the record is not found (or if the key is invalid). The statements following INVALID KEY are executed in such cases.
  • NOT INVALID KEY - This phrase specifies the steps to be taken if the deletion is successful and the key is valid.

Practical Example -


Scenario - Deleting a record from KSDS file using key (E0006).

Input File (KSDS) -

Browse           MATESY.EMPLOYEE.DETAILS
Command ===>                            
                            Type KSDS   
Key
<===>----10---+----2----+----3----+----4
****  Top of data  ****                 
E0001EMPLOYEE1      MANAGER   0000200000
E0002EMPLOYEE2      TL        0000150000
E0003EMPLOYEE3      SE        0000050000
E0004EMPLOYEE4      SSE       0000040000
E0005EMPLOYEE5      SE        0000045000
E0006EMPLOYEE6      SE        0000045000
****  End of data  ****

Code -

----+----1----+----2----+----3----+----4----+
       ...
       PROCEDURE DIVISION.

      * Opening file for DELETE
           OPEN I-O EMPFILE.

      * Deleting the record
           MOVE 'E0006'        TO EMP-ID.
           DELETE EMPFILE
                      INVALID KEY DISPLAY "RECORD NOT FOUND"
                  NOT INVALID KEY DISPLAY "RECORD SUCCESSFULLY DELETED" 
           END-DELETE.
      * Closing file
           CLOSE EMPFILE.
		   ...

Run JCL -

//MATESYF JOB MSGLEVEL=(1,1),NOTIFY=&SYSUID
//*                                                 
//STEP01  EXEC PGM=FILEDEL
//STEPLIB  DD  DSN=MATESY.COBOL.LOADLIB,DISP=SHR
//INPUT01  DD  DSN=MATESY.EMPLOYEE.DETAILS,DISP=SHR 
//SYSOUT   DD  SYSOUT=*

Output -

RECORD SUCCESSFULLY DELETED   

File after deletion -

Browse           MATESY.EMPLOYEE.DETAILS
Command ===>                            
                            Type KSDS   
Key                                     
<===>----10---+----2----+----3----+----4
****  Top of data  ****                 
E0001EMPLOYEE1      MANAGER   0000200000
E0002EMPLOYEE2      TL        0000150000
E0003EMPLOYEE3      SE        0000050000
E0004EMPLOYEE4      SSE       0000040000
E0005EMPLOYEE5      SE        0000045000
****  End of data  ****