在 Azure Data Factory 和 Azure Synapse Analytics 中刪除活動

適用於: Azure Data Factory Azure Synapse Analytics

提示

Fabric 中刪除活動的對應物是刪除資料活動。 關於設定細節,請參見 刪除資料活動。

你可以在 Azure Data Factory 使用刪除活動功能,從本地儲存儲存或雲端儲存儲存中刪除檔案或資料夾。 不再需要檔案時,使用此活動來清除或封存檔案。

警告

無法還原已刪除的檔案或資料夾 (除非儲存體已啟用虛刪除)。 使用 Delete 活動來刪除檔案或資料夾時請小心謹慎。

最佳作法

以下是使用 Delete 活動的一些建議:

  • 在使用 Delete 活動刪除檔案之前請先備份,您在未來可能需要還原這些檔案。

  • 請確定服務具有從儲存體存放區刪除資料夾或檔案的寫入權限。

  • 請確定您未在檔案寫入的同時刪除檔案。

  • 如果您想要從內部部署系統中刪除檔案或資料夾,請確定您使用大於 3.14 版的自我裝載整合執行階段。

支援的資料存放區

使用 UI 建立刪除動作

若要在管線中使用 Delete 活動,請完成下列步驟:

  1. 在管線活動窗格中搜尋 Delete,然後將 Delete 活動拖曳至管線畫布。

  2. 如果畫布上尚未選取新的刪除活動,請選取它,並選取其來源索引標籤以編輯詳細資料。

    顯示刪除活動的 UI。

  3. 選取現有或建立新的資料集,指定要刪除的檔案。 如果選取多個檔案,也可以啟用遞迴刪除,一併刪除任何子資料夾中的資料。 您也可以指定作業的並行連線數目上限。

  4. 可選擇性地設定記錄,方法是選取 [記錄設定] 索引標籤,然後選取目前的或建立新的記錄帳戶連結服務位置來記錄所執行刪除作業的結果。

    顯示「記錄設定」標籤以進行刪除活動。

語法

{
    "name": "DeleteActivity",
    "type": "Delete",
    "typeProperties": {
        "dataset": {
            "referenceName": "<dataset name>",
            "type": "DatasetReference"
        },
        "storeSettings": {
            "type": "<source type>",
            "recursive": true/false,
            "maxConcurrentConnections": <number>
        },
        "enableLogging": true/false,
        "logStorageSettings": {
            "linkedServiceName": {
                "referenceName": "<name of linked service>",
                "type": "LinkedServiceReference"
            },
            "path": "<path to save log file>"
        }
    }
}

類型屬性

屬性 描述 必要
資料集 提供資料集參考,以判斷要刪除哪些檔案或資料夾 Yes
遞迴 指出是否從子資料夾、或只有從指定的資料夾,以遞迴方式刪除檔案。 否。 預設值為 false。
maxConcurrentConnections(最大並發連線數) 同時連線到儲存體存放區以刪除資料夾或檔案的連線數。 否。 預設值為 1。
啟用記錄 指出您是否需要記錄已刪除的資料夾或檔案名稱。 如果為 true,則您需要進一步提供儲存體帳戶來儲存記錄檔,以便您可以藉由讀取記錄檔來追蹤 Delete 活動的行為。 否
日誌儲存設定 僅適用於 enablelogging = true 時。

一組儲存體屬性,可指定您要儲存記錄檔 (包含由 Delete 活動刪除的資料夾或檔案名稱) 的位置。
否
鏈接服務名稱 僅適用於 enablelogging = true 時。

連結服務 Azure 儲存體、Azure Data Lake Storage Gen1 或 Azure Data Lake Storage Gen2 用來儲存包含刪除活動中刪除的資料夾或檔案名稱的日誌檔。 請注意,其必須設定為與刪除活動用來刪除檔案的 Integration Runtime 相同類型。
否
路徑 僅適用於 enablelogging = true 時。

要在您的儲存體帳戶中儲存記錄檔的路徑。 如果不提供路徑,服務會為您建立容器。
否

監視

您可以在兩個位置查看及監視 Delete 活動的結果:

  • 從刪除活動的輸出。
  • 來自記錄檔的資料。

Delete 活動的範例輸出

{ 
  "datasetName": "AmazonS3",
  "type": "AmazonS3Object",
  "prefix": "test",
  "bucketName": "adf",
  "recursive": true,
  "isWildcardUsed": false,
  "maxConcurrentConnections": 2,  
  "filesDeleted": 4,
  "logPath": "https://sample.blob.core.windows.net/mycontainer/5c698705-a6e2-40bf-911e-e0a927de3f07",
  "effectiveIntegrationRuntime": "MyAzureIR (West Central US)",
  "executionDuration": 650
}

Delete 活動的範例記錄檔

名稱 類別 狀態 錯誤
test1/yyy.json 檔案 已刪除
test2/hello789.txt 檔案 已刪除
test2/test3/hello000.txt 檔案 已刪除
test2/test3/zzz.json 檔案 已刪除

使用 Delete 活動的範例

刪除特定資料夾或檔案

商店具有下列資料夾結構:

Root/
    Folder_A_1/
        1.txt
        2.txt
        3.csv
    Folder_A_2/
        4.txt
        5.csv
        Folder_B_1/
            6.txt
            7.csv
        Folder_B_2/
            8.txt

現在您正在使用 Delete 活動,透過從資料集和 Delete 活動的不同屬性值組合來刪除資料夾或檔案。

資料夾路徑 檔案名稱 遞迴 輸出
根目錄/ Folder_A_2 NULL 假 Root/
    Folder_A_1/
        1.txt
        2.txt
        3.csv
    Folder_A_2/
         4.txt
         5.csv
        Folder_B_1/
            6.txt
            7.csv
        Folder_B_2/
            8.txt
根目錄/ Folder_A_2 NULL True Root/
    Folder_A_1/
        1.txt
        2.txt
        3.csv
     Folder_A_2/
         4.txt
         5.csv
         Folder_B_1/
             6.txt
             7.csv
         Folder_B_2/
             8.txt
根目錄/ Folder_A_2 *.txt 假 Root/
    Folder_A_1/
        1.txt
        2.txt
        3.csv
    Folder_A_2/
         4.txt
        5.csv
        Folder_B_1/
            6.txt
            7.csv
        Folder_B_2/
            8.txt
根目錄/ Folder_A_2 *.txt True Root/
    Folder_A_1/
        1.txt
        2.txt
        3.csv
    Folder_A_2/
         4.txt
        5.csv
        Folder_B_1/
             6.txt
            7.csv
        Folder_B_2/
             8.txt

定期清除時間分割的資料夾或檔案

您可以建立管線以定期清除時間分割的資料夾或檔案。 例如,資料夾結構類似:/mycontainer/2018/12/14/*.csv。 您可以使用排程觸發程序中的服務系統的變數,來識別在每次管線執行中應該刪除的資料夾或檔案。

範例管線

{
    "name":"cleanup_time_partitioned_folder",
    "properties":{
        "activities":[
            {
                "name":"DeleteOneFolder",
                "type":"Delete",
                "dependsOn":[

                ],
                "policy":{
                    "timeout":"7.00:00:00",
                    "retry":0,
                    "retryIntervalInSeconds":30,
                    "secureOutput":false,
                    "secureInput":false
                },
                "userProperties":[

                ],
                "typeProperties":{
                    "dataset":{
                        "referenceName":"PartitionedFolder",
                        "type":"DatasetReference",
                        "parameters":{
                            "TriggerTime":{
                                "value":"@formatDateTime(pipeline().parameters.TriggerTime, 'yyyy/MM/dd')",
                                "type":"Expression"
                            }
                        }
                    },
                    "logStorageSettings":{
                        "linkedServiceName":{
                            "referenceName":"BloblinkedService",
                            "type":"LinkedServiceReference"
                        },
                        "path":"mycontainer/log"
                    },
                    "enableLogging":true,
                    "storeSettings":{
                        "type":"AzureBlobStorageReadSettings",
                        "recursive":true
                    }
                }
            }
        ],
        "parameters":{
            "TriggerTime":{
                "type":"string"
            }
        },
        "annotations":[

        ]
    }
}

範例資料集

{
    "name":"PartitionedFolder",
    "properties":{
        "linkedServiceName":{
            "referenceName":"BloblinkedService",
            "type":"LinkedServiceReference"
        },
        "parameters":{
            "TriggerTime":{
                "type":"string"
            }
        },
        "annotations":[

        ],
        "type":"Binary",
        "typeProperties":{
            "location":{
                "type":"AzureBlobStorageLocation",
                "folderPath":{
                    "value":"@dataset().TriggerTime",
                    "type":"Expression"
                },
                "container":{
                    "value":"mycontainer",
                    "type":"Expression"
                }
            }
        }
    }
}

範例觸發程序

{
    "name": "DailyTrigger",
    "properties": {
        "runtimeState": "Started",
        "pipelines": [
            {
                "pipelineReference": {
                    "referenceName": "cleanup_time_partitioned_folder",
                    "type": "PipelineReference"
                },
                "parameters": {
                    "TriggerTime": "@trigger().scheduledTime"
                }
            }
        ],
        "type": "ScheduleTrigger",
        "typeProperties": {
            "recurrence": {
                "frequency": "Day",
                "interval": 1,
                "startTime": "2018-12-13T00:00:00.000Z",
                "timeZone": "UTC",
                "schedule": {
                    "minutes": [
                        59
                    ],
                    "hours": [
                        23
                    ]
                }
            }
        }
    }
}

清除 2018.1.1 以前上次修改的到期檔案

您可以建立管線,藉由在資料集中利用檔案屬性篩選,清除舊的或已過期的檔案:"LastModified"。

範例管線

{
    "name":"CleanupExpiredFiles",
    "properties":{
        "activities":[
            {
                "name":"DeleteFilebyLastModified",
                "type":"Delete",
                "dependsOn":[

                ],
                "policy":{
                    "timeout":"7.00:00:00",
                    "retry":0,
                    "retryIntervalInSeconds":30,
                    "secureOutput":false,
                    "secureInput":false
                },
                "userProperties":[

                ],
                "typeProperties":{
                    "dataset":{
                        "referenceName":"BlobFilesLastModifiedBefore201811",
                        "type":"DatasetReference"
                    },
                    "logStorageSettings":{
                        "linkedServiceName":{
                            "referenceName":"BloblinkedService",
                            "type":"LinkedServiceReference"
                        },
                        "path":"mycontainer/log"
                    },
                    "enableLogging":true,
                    "storeSettings":{
                        "type":"AzureBlobStorageReadSettings",
                        "recursive":true,
                        "modifiedDatetimeEnd":"2018-01-01T00:00:00.000Z"
                    }
                }
            }
        ],
        "annotations":[

        ]
    }
}

範例資料集

{
    "name":"BlobFilesLastModifiedBefore201811",
    "properties":{
        "linkedServiceName":{
            "referenceName":"BloblinkedService",
            "type":"LinkedServiceReference"
        },
        "annotations":[

        ],
        "type":"Binary",
        "typeProperties":{
            "location":{
                "type":"AzureBlobStorageLocation",
                "fileName":"*",
                "folderPath":"mydirectory",
                "container":"mycontainer"
            }
        }
    }
}

以串連複製活動和刪除活動的方式移動檔案

你可以用 複製活動 複製檔案,再用 Delete 活動刪除管線中的檔案來移動檔案。 當你想移動多個檔案時,可以使用以下範例中的 GetMetadata 活動 + 篩選活動 + Foreach 活動 + 複製活動 + Delete 活動。

注意

如果你想透過定義一個只包含資料夾路徑的資料集,然後用 複製活動 和 Delete 活動來參考代表同一資料夾的資料集來移動整個資料夾,你就必須非常小心。 您必須確定不會有任何新檔案抵達複製作業與刪除作業之間的資料夾。 如果在您的複製活動剛完成複製作業但 Delete 活動尚未開始時,便有新檔案抵達資料夾,Delete 活動可能會因刪除整個資料夾而將尚未複製到目的地的新抵達檔案誤刪。

範例管線

{
    "name":"MoveFiles",
    "properties":{
        "activities":[
            {
                "name":"GetFileList",
                "type":"GetMetadata",
                "dependsOn":[

                ],
                "policy":{
                    "timeout":"7.00:00:00",
                    "retry":0,
                    "retryIntervalInSeconds":30,
                    "secureOutput":false,
                    "secureInput":false
                },
                "userProperties":[

                ],
                "typeProperties":{
                    "dataset":{
                        "referenceName":"OneSourceFolder",
                        "type":"DatasetReference",
                        "parameters":{
                            "Container":{
                                "value":"@pipeline().parameters.SourceStore_Location",
                                "type":"Expression"
                            },
                            "Directory":{
                                "value":"@pipeline().parameters.SourceStore_Directory",
                                "type":"Expression"
                            }
                        }
                    },
                    "fieldList":[
                        "childItems"
                    ],
                    "storeSettings":{
                        "type":"AzureBlobStorageReadSettings",
                        "recursive":true
                    },
                    "formatSettings":{
                        "type":"BinaryReadSettings"
                    }
                }
            },
            {
                "name":"FilterFiles",
                "type":"Filter",
                "dependsOn":[
                    {
                        "activity":"GetFileList",
                        "dependencyConditions":[
                            "Succeeded"
                        ]
                    }
                ],
                "userProperties":[

                ],
                "typeProperties":{
                    "items":{
                        "value":"@activity('GetFileList').output.childItems",
                        "type":"Expression"
                    },
                    "condition":{
                        "value":"@equals(item().type, 'File')",
                        "type":"Expression"
                    }
                }
            },
            {
                "name":"ForEachFile",
                "type":"ForEach",
                "dependsOn":[
                    {
                        "activity":"FilterFiles",
                        "dependencyConditions":[
                            "Succeeded"
                        ]
                    }
                ],
                "userProperties":[

                ],
                "typeProperties":{
                    "items":{
                        "value":"@activity('FilterFiles').output.value",
                        "type":"Expression"
                    },
                    "batchCount":20,
                    "activities":[
                        {
                            "name":"CopyAFile",
                            "type":"Copy",
                            "dependsOn":[

                            ],
                            "policy":{
                                "timeout":"7.00:00:00",
                                "retry":0,
                                "retryIntervalInSeconds":30,
                                "secureOutput":false,
                                "secureInput":false
                            },
                            "userProperties":[

                            ],
                            "typeProperties":{
                                "source":{
                                    "type":"BinarySource",
                                    "storeSettings":{
                                        "type":"AzureBlobStorageReadSettings",
                                        "recursive":false,
                                        "deleteFilesAfterCompletion":false
                                    },
                                    "formatSettings":{
                                        "type":"BinaryReadSettings"
                                    },
                                    "recursive":false
                                },
                                "sink":{
                                    "type":"BinarySink",
                                    "storeSettings":{
                                        "type":"AzureBlobStorageWriteSettings"
                                    }
                                },
                                "enableStaging":false,
                                "dataIntegrationUnits":0
                            },
                            "inputs":[
                                {
                                    "referenceName":"OneSourceFile",
                                    "type":"DatasetReference",
                                    "parameters":{
                                        "Container":{
                                            "value":"@pipeline().parameters.SourceStore_Location",
                                            "type":"Expression"
                                        },
                                        "Directory":{
                                            "value":"@pipeline().parameters.SourceStore_Directory",
                                            "type":"Expression"
                                        },
                                        "filename":{
                                            "value":"@item().name",
                                            "type":"Expression"
                                        }
                                    }
                                }
                            ],
                            "outputs":[
                                {
                                    "referenceName":"OneDestinationFile",
                                    "type":"DatasetReference",
                                    "parameters":{
                                        "Container":{
                                            "value":"@pipeline().parameters.DestinationStore_Location",
                                            "type":"Expression"
                                        },
                                        "Directory":{
                                            "value":"@pipeline().parameters.DestinationStore_Directory",
                                            "type":"Expression"
                                        },
                                        "filename":{
                                            "value":"@item().name",
                                            "type":"Expression"
                                        }
                                    }
                                }
                            ]
                        },
                        {
                            "name":"DeleteAFile",
                            "type":"Delete",
                            "dependsOn":[
                                {
                                    "activity":"CopyAFile",
                                    "dependencyConditions":[
                                        "Succeeded"
                                    ]
                                }
                            ],
                            "policy":{
                                "timeout":"7.00:00:00",
                                "retry":0,
                                "retryIntervalInSeconds":30,
                                "secureOutput":false,
                                "secureInput":false
                            },
                            "userProperties":[

                            ],
                            "typeProperties":{
                                "dataset":{
                                    "referenceName":"OneSourceFile",
                                    "type":"DatasetReference",
                                    "parameters":{
                                        "Container":{
                                            "value":"@pipeline().parameters.SourceStore_Location",
                                            "type":"Expression"
                                        },
                                        "Directory":{
                                            "value":"@pipeline().parameters.SourceStore_Directory",
                                            "type":"Expression"
                                        },
                                        "filename":{
                                            "value":"@item().name",
                                            "type":"Expression"
                                        }
                                    }
                                },
                                "logStorageSettings":{
                                    "linkedServiceName":{
                                        "referenceName":"BloblinkedService",
                                        "type":"LinkedServiceReference"
                                    },
                                    "path":"container/log"
                                },
                                "enableLogging":true,
                                "storeSettings":{
                                    "type":"AzureBlobStorageReadSettings",
                                    "recursive":true
                                }
                            }
                        }
                    ]
                }
            }
        ],
        "parameters":{
            "SourceStore_Location":{
                "type":"String"
            },
            "SourceStore_Directory":{
                "type":"String"
            },
            "DestinationStore_Location":{
                "type":"String"
            },
            "DestinationStore_Directory":{
                "type":"String"
            }
        },
        "annotations":[

        ]
    }
}

範例資料集

GetMetadata 活動使用資料集來列舉檔案清單。

{
    "name":"OneSourceFolder",
    "properties":{
        "linkedServiceName":{
            "referenceName":"AzureStorageLinkedService",
            "type":"LinkedServiceReference"
        },
        "parameters":{
            "Container":{
                "type":"String"
            },
            "Directory":{
                "type":"String"
            }
        },
        "annotations":[

        ],
        "type":"Binary",
        "typeProperties":{
            "location":{
                "type":"AzureBlobStorageLocation",
                "folderPath":{
                    "value":"@{dataset().Directory}",
                    "type":"Expression"
                },
                "container":{
                    "value":"@{dataset().Container}",
                    "type":"Expression"
                }
            }
        }
    }
}

複製活動和刪除活動使用的資料來源資料集。

{
    "name":"OneSourceFile",
    "properties":{
        "linkedServiceName":{
            "referenceName":"AzureStorageLinkedService",
            "type":"LinkedServiceReference"
        },
        "parameters":{
            "Container":{
                "type":"String"
            },
            "Directory":{
                "type":"String"
            },
            "filename":{
                "type":"string"
            }
        },
        "annotations":[

        ],
        "type":"Binary",
        "typeProperties":{
            "location":{
                "type":"AzureBlobStorageLocation",
                "fileName":{
                    "value":"@dataset().filename",
                    "type":"Expression"
                },
                "folderPath":{
                    "value":"@{dataset().Directory}",
                    "type":"Expression"
                },
                "container":{
                    "value":"@{dataset().Container}",
                    "type":"Expression"
                }
            }
        }
    }
}

複製活動使用的資料目的地資料集。

{
    "name":"OneDestinationFile",
    "properties":{
        "linkedServiceName":{
            "referenceName":"AzureStorageLinkedService",
            "type":"LinkedServiceReference"
        },
        "parameters":{
            "Container":{
                "type":"String"
            },
            "Directory":{
                "type":"String"
            },
            "filename":{
                "type":"string"
            }
        },
        "annotations":[

        ],
        "type":"Binary",
        "typeProperties":{
            "location":{
                "type":"AzureBlobStorageLocation",
                "fileName":{
                    "value":"@dataset().filename",
                    "type":"Expression"
                },
                "folderPath":{
                    "value":"@{dataset().Directory}",
                    "type":"Expression"
                },
                "container":{
                    "value":"@{dataset().Container}",
                    "type":"Expression"
                }
            }
        }
    }
}

您還可以取得範本以從這裡移動檔案。

已知的限制

  • Delete 活動不支援刪除以萬用字元說明的資料夾清單。

  • 在 Delete 活動中使用檔案屬性篩選器:modifiedDatetimeStart 和 modifiedDatetimeEnd 以選取要刪除的檔案時,也請務必在刪除活動中設定 "wildcardFileName":"*"。

了解更多關於在 Azure Data Factory 和 Synapse 管線中移動檔案的資訊。