DELETE - Deleting from an Internal Table

Variants:

1. DELETE itab.

2. DELETE TABLE itab WITH TABLE KEY k1 = v1 ... kn = vn.

3. DELETE TABLE itab [FROM wa].

4. DELETE itab INDEX idx.

5. DELETE itab FROM idx1 TO idx2.

6. DELETE itab WHERE logexp.

7. DELETE ADJACENT DUPLICATES FROM itab.

See Short forms of line operations not allowed.

Effect

Deletes one or more lines from an internal table.

Note

If you delete lines within a LOOP ... ENDLOOP block, the deletion affects subsequent loop passes.

Variant 1

DELETE itab.


Effect

Deletes the current entry from an internal table in a LOOP. You can only use this variant with index tables (standard or sorted tables).

Return Code is set to 0.

Note

Once you have deleted the current entry of an internal table in a LOOP, the effect of subsequent changes to the current entry without specifying the INDEX cannot be guaranteed, and the behavior may change in future releases.

Variant 2

DELETE TABLE itab WITH TABLE KEY k1 = v1 ... kn = vn.


Effect

Deletes generically from an internal table. You can use this variant for any tables. The variant is a single command that deletes the table line with the key values k1 = v1 ....kn = vn. If there is more than one line with the specified key, only the first one is deleted. The table key must be specified fully. If the type of a value and the type of the associated key are not compatible, the system converts the value into the type of the component before the statement is executed, using MOVE logic. If a component is not known until runtime, you can specify it dynamically as the contents of the field fi in the expression WITH TABLE KEY ... (fi) = vi ....

The costs for deleting one entry depend on the table type with which the table is defined:

SY-SUBRC = 0:
Entry deleted.
SY-SUBRC = 4:
Entry did not exist.

Variant 3

DELETE TABLE itab [FROM wa].


Effect

Behaves similarly to variant 2. The values for the table key are taken from the corresponding components of the (structured) field wa. The field must be compatible with the table line of itab. This method allows you to delete from a table without the table key having to be known in advance. If the internal table has a header line, you can leave out the FROM wa addition; the system then takes the key values from the header line.

Variant 4

DELETE itab INDEX idx.


Effect

Deletes the idx-th entry of the internal table itab. This variant is only allowed with index tables (standard or sorted tables).

The Return Code is set as follows:

SY-SUBRC = 0:
Entry deleted.
SY-SUBRC = 4:
Entry did not exist.

Variant 5

DELETE itab FROM idx1 TO idx2.


Effect

Deletes the range of lines from index idx1 to idx2 from the internal table itab. You can only use this variant with index tables (standard or sorted tables). You must specify at least one of the parameters FROM idx1 or TO idx2. If you omit the FROM parameter, the system deletes the lines from the start of the table to idx2. If you omit the TO parameter, the system deletes from line idx1 to the end of the table. The starting line idx1 must be greater than 0.

The Return Code is set as follows:

SY-SUBRC = 0:
At least one entry deleted.
SY-SUBRC = 4:
No entries deleted.

Variant 6

DELETE itab WHERE logexp.

Extras:

1. ... FROM idx1

2. ... TO   idx2

Effect

Deletes all of the entries from the internal table itab that satisfy the condition logexp. The condition logexp can be almost any logical expression. The only restriction is that the first field in each comparison must be a component of the line structure of the internal table itab.

The Return Code is set as follows:

SY-SUBRC = 0:
At least one entry deleted.
SY-SUBRC = 4:
No entries deleted.

Notes

  1. When you use the WHERE condition with a STANDARD or HASHED table, a full table scan is always required.

    If you are working with a SORTED TABLE, the partial sequential processing can be optimized so that only the entries that actually satisfy the WHERE condition are processed. This optimization requires that each sub-condition is linked with AND; that no parentheses are used; that at least one sub-condition is in the form k = v; and that the conditions in the form k1 = v1 ... kn = vn cover at least the first part of the table key.

    The starting point for the loop is specified using a binary search with the sub-conditions that cover the table key (partially or completely). This optimization is similar to the optimization in the READ statement. The loop is only processed from the starting point to the point where these sub-conditions are still met. (See also Optimized Key Operations in Internal Tables).

  2. Each operand in the WHERE condition that is not a component of the corresponding internal table itab can be replaced by a functional method call. Functional methods are methods in ABAP Objects with any number of IMPORTING parameters and a single RETURNING parameter.


  3. If the line type of the internal table contains object reference variables, or the entire line type is itself a reference variable, you can use the attributes of the object to which the reference is pointing as comparison values in the WHERE condition (see Attributes of objects as the key of an internal table).


  4. You can only use the additions ... FROM idx1 and ... TO idx2 with index tables (standard or sorted tables).


Addition 1

... FROM idx1

Effect

Restricts the area searched to the set of lines beginning at index idx1. If you omit the addition FROM idx1, the system searches from the start of the table.
The FROM addition must occur before the WHERE condition.

Addition 2

... TO   idx2

Effect

Restricts the area searched to the set of lines up to index idx2. If you omit the addition TO idx2, the system searches to the end of the table.
The TO addition must occur befor the WHERE condition.

Example

Example to delete all of the lines between 5 and 36 in a table of names where the entry begins with one of the letters 'A' to 'C'.

TYPES: BEGIN OF NAMETAB_TYPE,
         NAME(30) TYPE C,
       END OF NAMETAB_TYPE.

DATA: NAMETAB TYPE STANDARD TABLE OF NAMETAB_TYPE WITH
                   NON-UNIQUE DEFAULT KEY INITIAL SIZE 100.
...
DELETE NAMETAB FROM 5 TO 36 WHERE NAME CA 'ABC'.

Variant 7

DELETE ADJACENT DUPLICATES FROM itab.


Extras:

1. ... COMPARING f1 f2 ...

2. ... COMPARING ALL FIELDS

Effect

Deletes adjacent duplicate entries from the internal table itab. If there are n duplicate entries in succession, the first entry is retained, and the following n-1 entries are deleted.

Two lines are regarded as duplicates if their keys are identical.

The Return Code is set as follows:

SY-SUBRC = 0:
At least one duplicate was found, and at least one entry was deleted.
SY-SUBRC = 4:
No duplicates found, no entries deleted.

Addition 1

... COMPARING f1 f2 ...

Effect

Two lines of the internal table itab are regarded as duplicates if they have identical contents in the fields f1, f2, ...

Addition 2

... COMPARING ALL FIELDS

Effect

Two lines of the internal table are regarded as duplicates if all of their field contents are identical.

Notes

  1. The DELETE ADJACENT DUPLICATES statement works particularly well if you have sorted the internal table itab according to the fields that you want to compare when looking for duplicates. In this case, deleting adjacent duplicates is the same as deleting all duplicates. The direction of the sort is irrelevant.


  2. If you do not know a comparison expression until runtime, you can specify it dynamically as the contents of the field name in the expression COMPARING ... (name) .... If name is empty at runtime, the comparison expression is ignored. If name contains an invalid component name, a runtime error occurs.


  3. You can further restrict comparison expressions - both static and dynamic - by specifying offset and length.


Note

Performance:

  1. When you delete a line of an internal table, index maintenance costs are incurred. These depend on the index of the table. The runtime is independent of the width of the table line.

    Deleting a line from the middle of an internal table with 200 entries usually requires around 10 msn (standardized microseconds).

    When you delete a set of entries using "DELETE itab FROM idx1 TO idx2." or "DELETE itab WHERE ...", index maintenance costs are only incurred once. This means that it is considerably more efficient than deleting entries in a LOOP.

  2. If you want to delete adjacent duplicate entries from an internal table, use the variant " DELETE ADJACENT DUPLICATES FROM itab." instead of a LOOP construction.


Related

INSERT itab, MODIFY itab

Additional help

Deleting Table Lines

Deleting Table Lines Using the Index