Usina BRAdvPL Guide
Sign in
← All topics
FOUNDATIONPublished

D_E_L_E_T_ field and logical deletion in Protheus

D_E_L_E_T_ = espaço → registro ativo | D_E_L_E_T_ = "*" → registro excluído logicamente

Understand how Protheus represents logical deletion in SQL databases and why queries must filter D_E_L_E_T_ on each table.

DatabaseSQLDBAccessTopConnLogical deletionD_E_L_E_T_
01 · OVERVIEW

Overview

In Protheus, the standard deletion model is logical: the row remains physically stored, but the D_E_L_E_T_ field marks it as no longer considered by the application. Direct SQL queries must handle this explicitly, otherwise the database may return rows that Protheus treats as deleted. The historical ADVPL X SQL programming material also relates D_E_L_E_T_ to R_E_C_N_O_ and R_E_C_D_E_L_, DBAccess control fields used for ISAM compatibility and unique-key handling after logical deletion.

02 · SYNTAX

Syntax

D_E_L_E_T_ = espaço → registro ativo | D_E_L_E_T_ = "*" → registro excluído logicamente

Parameters

D_E_L_E_T_
Campo de controleRequired

Logical-deletion flag. Blank means active row; asterisk means logically deleted row.

R_E_C_N_O_
Campo de controleOptional

Physical identifier used for positioning and Recno() compatibility in SQL environments.

R_E_C_D_E_L_
Campo de controleOptional

Supports unique-key composition for logically deleted rows by receiving R_E_C_N_O_ on deletion.

Return value

Not a function; it is a persistence convention in Protheus database tables.

03 · PRACTICAL EXAMPLE

Simple query returning only active rows

#Include "TOTVS.ch"
#Include "TopConn.ch"

User Function ExDelLogico()
    Local cAlias := GetNextAlias()
    Local cQuery := ""

    cQuery := "SELECT SA1.A1_COD, SA1.A1_LOJA, SA1.A1_NOME "
    cQuery += "FROM " + RetSqlName("SA1") + " SA1 "
    cQuery += "WHERE SA1.A1_FILIAL = '" + xFilial("SA1") + "' "
    cQuery += "AND SA1.D_E_L_E_T_ = ' ' "
    cQuery += "ORDER BY SA1.A1_COD, SA1.A1_LOJA"
    cQuery := ChangeQuery(cQuery)

    TCQUERY (cQuery) ALIAS (cAlias) NEW

    While !(cAlias)->(Eof())
        ConOut((cAlias)->A1_COD + " - " + (cAlias)->A1_NOME)
        (cAlias)->(DbSkip())
    EndDo

    (cAlias)->(DbCloseArea())
Return
Expected result

The query returns only active customers for the correct branch and closes the temporary work area.

04 · PRACTICAL EXAMPLE

JOIN filtering logical deletion on each table

cQuery := "SELECT SA1.A1_COD, SA1.A1_NOME, SE1.E1_NUM, SE1.E1_SALDO "
cQuery += "FROM " + RetSqlName("SA1") + " SA1 "
cQuery += "INNER JOIN " + RetSqlName("SE1") + " SE1 "
cQuery += "ON SE1.E1_FILIAL = '" + xFilial("SE1") + "' "
cQuery += "AND SE1.E1_CLIENTE = SA1.A1_COD "
cQuery += "AND SE1.E1_LOJA = SA1.A1_LOJA "
cQuery += "AND SE1.D_E_L_E_T_ = ' ' "
cQuery += "WHERE SA1.A1_FILIAL = '" + xFilial("SA1") + "' "
cQuery += "AND SA1.D_E_L_E_T_ = ' ' "
Expected result

The filter is applied to both SA1 and SE1, avoiding a mix of active and logically deleted rows.

05 · PRACTICAL EXAMPLE

R_E_C_D_E_L_ and key reuse

// Conceito simplificado:
// CODIGO  R_E_C_N_O_  D_E_L_E_T_  R_E_C_D_E_L_
// 001     1           "*"          1
// 001     2           " "          0
Expected result

The logically deleted row remains stored, while R_E_C_D_E_L_ helps release the unique key for a new active insertion.

BEST PRACTICES
  • Filter logical deletion on every Protheus table queried directly with SQL.
  • Qualify D_E_L_E_T_ with the table alias.
  • Use RetSqlName() and ChangeQuery() for physical names and database portability.
  • Treat logical deletion as different from physical deletion.
COMMON PITFALLS
  • Forgetting D_E_L_E_T_ may return rows Protheus considers deleted.
  • Filtering only the main table is not enough when joining Protheus tables.
  • Physical DELETE directly in the database may break traceability and standard routines.
  • Do not manually update D_E_L_E_T_, R_E_C_N_O_ or R_E_C_D_E_L_ without controlled validation.

Related content

REFERENCES
  1. MICROSIGA. ADVPL Programming — Query usage. ADVPL X SQL Programming document, supplied to the project, Aug. 27, 2006. "Protheus particularities" section.
  2. TOTVS. ADVPL DML commands in SQL/DBAccess. TOTVS Support Center.
  3. TOTVS. Developing queries in Protheus. TDN.
  4. TOTVS. TCQUERY command. TDN.
Status
Published
Page created on
Last reviewed on
Original language
Portuguese
Reviewed by
Usina.BR
0 approved comment(s)

Comments

There are no approved comments yet.

Sign in with Google or Microsoft to comment.

Powered by Usina Docs · Alpha