CLEAR in ABAP: Initialize Variables, Structures and Tables – Syntax and Usage

Category
ABAP-Statements
Published
Author
Johannes

The CLEAR statement is one of the most fundamental and frequently used statements in ABAP. Its purpose is to reset the content of a data object (a variable) to its type-appropriate initial value. It “clears” or “empties” the current value of the variable and restores the state the variable would have directly after its declaration (without VALUE addition).

CLEAR is applicable to almost all types of data objects:

  • Elementary variables (numbers, text, date, time, etc.)
  • Structures (work areas)
  • Internal tables
  • Reference variables (object references, data references)
  • Field symbols (affects the assigned data object)

Syntax

The syntax is very simple:

1. Reset a Single Data Object

CLEAR <data_object>.
  • <data_object>: The name of the variable, structure, internal table, etc. to be reset.

2. Reset Multiple Data Objects (Chain Statement)

CLEAR: <data_object1>, <data_object2>, ..., <data_objectN>.

With a colon after CLEAR and commas between the variable names, multiple data objects can be reset in one statement.

Behavior and Effect on Different Data Types

The initial value to which CLEAR resets depends on the data type of the data object:

Elementary Types

  • Numeric (I, P, F, …): 0
  • Fixed-length character string (C, N, D, T): Filled with spaces (according to the defined length). E.g., C(5) becomes ' '.
  • Variable-length character string (STRING): Empty string ('').
  • Fixed-length byte string (X): Filled with hexadecimal 00 (according to the defined length). E.g., X(2) becomes X'0000'.
  • Variable-length byte string (XSTRING): Empty byte string.
  • References (TYPE REF TO ...): The reference becomes NULL (it no longer points to any object or data area).

Structures

CLEAR affects each component of the structure individually, according to the data type of each component. Clearing a structure means setting all its fields to their initial values.

Internal Tables

CLEAR <internal_table>. deletes all rows from the table body. The table is empty afterwards (contains 0 rows). The memory occupied by the rows is released (except for any base memory reserved by INITIAL SIZE).

  • Note on tables with header line (obsolete): For tables still declared with the old DATA <table> TYPE <type> OCCURS n WITH HEADER LINE. syntax, CLEAR <table>. resets both the table body and the header line. CLEAR <table>[]. would only clear the body. With modern table declarations (DATA <table> TYPE TABLE OF ...), there is no header line, and CLEAR <table>. always clears the body.

Distinction from FREE

Although CLEAR releases the memory of rows for internal tables, there is also the FREE statement.

  • CLEAR: Resets the value. For tables: Deletes row content and releases row memory.
  • FREE: Is more aggressive with memory release, especially for internal tables and objects like strings. FREE <table>. attempts to return the entire memory of the table (including management information) to the operating system. FREE also resets the variable to its initial value. FREE is typically used when very large tables or objects are definitely no longer needed and the memory should urgently be released. For simple, elementary variables, FREE behaves like CLEAR.

Typical Use Cases for CLEAR

  • Initialize a variable before (re-)use to ensure no old values are present.
  • Reset a work area (work area) before reading data from a new row of an internal table (important in LOOP AT ... INTO wa. loops).
  • Empty an internal table before filling it with new data.
  • Reset counters, sums, or status indicators.

Examples

1. Elementary Variables

DATA count TYPE i VALUE 55.
DATA description TYPE string VALUE 'Some text'.
DATA is_valid TYPE abap_bool VALUE abap_true.
WRITE: / 'Before:', count, description, is_valid.
CLEAR: count, description, is_valid.
WRITE: / 'After:', count, description, is_valid.

Output:

Before: 55 Some text X
After: 0 ' '

(Note: abap_false is displayed as space ’ ‘).

2. Structure

TYPES: BEGIN OF ty_person,
name TYPE string,
age TYPE i,
END OF ty_person.
DATA person TYPE ty_person.
person-name = 'Smith'.
person-age = 42.
WRITE: / 'Person before:', person-name, person-age.
CLEAR person.
WRITE: / 'Person after:', person-name, person-age.

Output:

Person before: Smith 42
Person after: 0

3. Internal Table

DATA number_table TYPE STANDARD TABLE OF i WITH EMPTY KEY.
APPEND 1 TO number_table.
APPEND 2 TO number_table.
APPEND 3 TO number_table.
WRITE: / 'Rows before CLEAR:', lines( number_table ).
CLEAR number_table.
WRITE: / 'Rows after CLEAR:', lines( number_table ).

Output:

Rows before CLEAR: 3
Rows after CLEAR: 0

4. Work Area in a Loop

TYPES: BEGIN OF ty_item,
id TYPE i,
END OF ty_item.
DATA: lt_items TYPE STANDARD TABLE OF ty_item,
ls_item TYPE ty_item.
APPEND VALUE #( id = 10 ) TO lt_items.
APPEND VALUE #( id = 20 ) TO lt_items.
LOOP AT lt_items INTO ls_item.
WRITE: / 'Processing ID:', ls_item-id.
" ... logic with ls_item ...
" Good practice: Clear work area for the next iteration or later use
CLEAR ls_item.
WRITE: ' | Work area ID after CLEAR:', ls_item-id.
ENDLOOP.

Output:

Processing ID: 10 | Work area ID after CLEAR: 0
Processing ID: 20 | Work area ID after CLEAR: 0

In summary: CLEAR is an indispensable statement for resetting data objects to their defined initial state in a controlled manner. It is easy to use and essential for clean data processing in many ABAP programming scenarios.