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

適用於: Azure Data Factory Azure Synapse Analytics

提示

試用 Microsoft Fabric 中的 Data Factory,這是適用於企業的單一分析解決方案。 Microsoft Fabric 涵蓋從數據移動到數據科學、即時分析、商業智慧和報告等所有專案。 瞭解如何 免費啟動新的試用版

您可以使用 Azure Data Factory 中的刪除活動,從內部部署記憶體存放區或雲端記憶體存放區中刪除檔案或資料夾。 使用此活動在不再需要檔案時清除或封存盤案。

警告

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

最佳作法

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

  • 使用 Delete 活動刪除檔案之前先備份您的檔案,以防您日後需要加以還原。

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

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

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

支援的數據存放區

使用UI建立刪除活動

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

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

  2. 如果尚未選取新的 [刪除] 活動,請 選取畫布上的新 [刪除] 活動及其 [來源 ] 索引標籤,以編輯其詳細數據。

    Shows the UI for a Delete activity.

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

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

    Shows the  Logging settings  tab for a Delete activity.

語法

{
    "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
maxConcurrent 連線 ions 要同時連線到記憶體存放區的連線數目,以刪除資料夾或檔案。 否。 預設值為 1
啟用記錄 指出您是否需要記錄已刪除的資料夾或檔案名。 如果為 true,您需要進一步提供記憶體帳戶來儲存記錄檔,以便您可以讀取記錄檔來追蹤 Delete 活動的行為。 No
log 儲存體設定 只有在 enablelogging = true 時才適用。

一組記憶體屬性,可指定您想要儲存包含 Delete 活動所刪除之資料夾或檔案名的記錄檔。
No
linkedServiceName 只有在 enablelogging = true 時才適用。

Azure 儲存體、Azure Data Lake 儲存體 Gen1Azure Data Lake 儲存體 Gen2 的連結服務,用來儲存包含 Delete 活動所刪除之資料夾或檔名的記錄檔。 請注意,它必須以與刪除活動用來刪除檔案的 Integration Runtime 相同類型進行設定。
No
path 只有在 enablelogging = true 時才適用。

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

監視

您可以在下列兩個地方看到並監視Delete活動的結果:

  • 從 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
}

刪除活動的範例記錄檔

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

使用Delete活動的範例

刪除特定資料夾或檔案

存放區具有下列資料夾結構:

根/
    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活動的不同屬性值組合來刪除資料夾或檔案:

folderPath fileName 遞迴 輸出
根/Folder_A_2 NULL False 根/
    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 根/
    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 False 根/
    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 根/
    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 活動來移動檔案

您可以使用 複製活動 來複製檔案,然後移動 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"
                }
            }
        }
    }
}

複製活動和 Delete 活動所使用的數據源數據集。

{
    "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"
                }
            }
        }
    }
}

您也可以取得範本,從 此處移動檔案。

已知的限制

  • 刪除活動不支援刪除通配符所描述的資料夾清單。

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

深入瞭解如何在 Azure Data Factory 和 Synapse 管線中移動檔案。