---
id: guideline/microsoft/knowledge/data-modeling/scope-sales-header-attachment-cleanup-by-document-type
type: guideline
title: Scope sales header attachment cleanup by document type
summary: In the BC 29 source, a Sales Header is identified by Document Type and No., and its Document Attachment records carry the table ID, document number, and attachment document type. A quote and an order can therefore have the same number without being the same owner.
tier: official
language: en
tags:
  - document-attachment
  - sales-header
  - document-type
  - document-number
  - deleteall
  - attachment-cleanup
system: development
review:
  state: derived
  by: null
  at: null
  flags: []
generated:
  at: "2026-12-02T00:15:44.908Z"
  pipeline: 0.2.0
  prompts: {}
  input_hash: c6e97223e1933a606dd51d36fd6d797652cd796d
evidence:
  - kind: guideline
    url: https://github.com/microsoft/BCQuality/blob/main/microsoft/knowledge/data-modeling/scope-sales-header-attachment-cleanup-by-document-type.md
    title: "microsoft/BCQuality: Scope sales header attachment cleanup by document type"
    date: "2026-11-30"
    commit: 863acaa2bcbd27d73e72d02527c65c8dc5af5d01
    t: null
    quote: null
links:
  learn: []
  objects: []
  features: []
  topics: []
  localizations: []
  videos: []
  posts: []
  guidelines: []
  changes:
    - change/bcquality/290
  related:
    - to: object/table/1173
      title: table 1173 "Document Attachment"
      label: possibly related
      why: a code span in the rule names it
      by: code
    - to: object/codeunit/1173
      title: codeunit 1173 "Document Attachment Mgmt"
      label: possibly related
      why: a code span in the rule names it
      by: code
repo_path: microsoft/knowledge/data-modeling/scope-sales-header-attachment-cleanup-by-document-type.md
layer: microsoft
kind: rule
domain: data-modeling
bc_version:
  - "29"
technologies:
  - al
countries:
  - w1
application_area:
  - all
examples: 2
identifiers:
  - RecordRef
  - DeleteAttachedDocuments
---

# Scope sales header attachment cleanup by document type

> In the BC 29 source, a Sales Header is identified by Document Type and No., and its Document Attachment records carry the table ID, document number, and attachment document type. A quote and an order can therefore have the same number without being the same owner.

[Rule on GitHub](https://github.com/microsoft/BCQuality/blob/main/microsoft/knowledge/data-modeling/scope-sales-header-attachment-cleanup-by-document-type.md) · microsoft layer · data-modeling · BC 29 · tier official · derived

## Description

In the BC 29 source, a `Sales Header` is identified by `Document Type` and `No.`, and its `Document Attachment` records carry the table ID, document number, and attachment document type. A quote and an order can therefore have the same number without being the same owner. Custom cleanup filtered only by table ID and number can delete both owners' attachments.

## Best Practice

For cleanup intended for one sales header, pass its `RecordRef` to `Document Attachment Mgmt`.`DeleteAttachedDocuments`, which applies the owner filters before deleting. If custom filtering is necessary, include the matching document type as well as the table ID and number, and retain any additional filters required by the actual operation and extensions. Do not assume the same dimensions identify every other table's attachments. Deletion can also affect external storage through installed extensions; verify that lifecycle separately.

See sample: [`scope-sales-header-attachment-cleanup-by-document-type.good.al`](https://github.com/microsoft/BCQuality/blob/main/microsoft/knowledge/data-modeling/scope-sales-header-attachment-cleanup-by-document-type.good.al).

## Anti Pattern

Custom code deletes `Document Attachment` records for one `Sales Header` using only `Table ID` and `No.`, without restricting `Document Type`. It can remove attachments for a different sales document with the same number. Do not flag a deliberately cross-type operation whose intended scope includes those documents, or code that already constrains the type.

See sample: [`scope-sales-header-attachment-cleanup-by-document-type.bad.al`](https://github.com/microsoft/BCQuality/blob/main/microsoft/knowledge/data-modeling/scope-sales-header-attachment-cleanup-by-document-type.bad.al).

## References

- [BCApps: Sales Header primary key](https://github.com/microsoft/BCApps/blob/748fdaa43f5b0b95f6f4add417882d0eecc4b812/src/Layers/W1/BaseApp/Sales/Document/SalesHeader.Table.al).
- [BCApps: attachment identity and initialization](https://github.com/microsoft/BCApps/blob/748fdaa43f5b0b95f6f4add417882d0eecc4b812/src/Layers/W1/BaseApp/Foundation/Attachment/DocumentAttachment.Table.al).
- [BCApps: attachment management and owner filters](https://github.com/microsoft/BCApps/blob/748fdaa43f5b0b95f6f4add417882d0eecc4b812/src/Layers/W1/BaseApp/Foundation/Attachment/DocumentAttachmentMgmt.Codeunit.al).
- [BCApps: external storage deletion subscriber](https://github.com/microsoft/BCApps/blob/748fdaa43f5b0b95f6f4add417882d0eecc4b812/src/Apps/W1/External%20Storage%20-%20Document%20Attachments/app/src/DocumentAttachmentIntegration/DAExternalStorageImpl.Codeunit.al).
- [External attachment storage](https://learn.microsoft.com/dynamics365/business-central/across-store-document-attachments-externally): files can be outside the database; do not assume record deletion and external file deletion have identical effects.

## Examples

- [Good: scope-sales-header-attachment-cleanup-by-document-type.good.al](https://github.com/microsoft/BCQuality/blob/main/microsoft/knowledge/data-modeling/scope-sales-header-attachment-cleanup-by-document-type.good.al)
- [Bad: scope-sales-header-attachment-cleanup-by-document-type.bad.al](https://github.com/microsoft/BCQuality/blob/main/microsoft/knowledge/data-modeling/scope-sales-header-attachment-cleanup-by-document-type.bad.al)

## Names

- [table 1173 "Document Attachment"](../../../../objects/table/1173.md) (possibly related: a code span names it)
- [codeunit 1173 "Document Attachment Mgmt"](../../../../objects/codeunit/1173.md) (possibly related: a code span names it)

## Changed by

- [#290 Attachment cleanup by document type](../../../../changes/bcquality/290.md), 2026-11-30

---

From microsoft/BCQuality at 863acaa, MIT License, Copyright (c) 2026 Microsoft Corporation ([license](../../../LICENSE-BCQuality.txt)).
