filedatasetdelete-filepalantir-foundrypalantir-foundry-api

How to delete a file from dataset view in Foundry?


I have several files in the dataset view:

enter image description here

How to delete "test.json"? UI seems not to offer such possibility. How to do it using API?


Solution

  • It's possible using several Catalog API calls:
    startTransaction
    setTransactionType
    addFilesToDeleteTransaction
    commitTransaction

    import requests
    
    token = 'your_token'
    fullhost = 'https://your_hostname'
    dataset_rid = 'ri.foundry.main.dataset.d2c177b6-5331-4fa1-b09c-bc9b2d18b292'
    branch_id = 'master'
    file = 'test.json'
    headers={
        'content-type': 'application/json',
        'Authorization': 'Bearer ' + token
    }
    
    # Start transaction
    response = requests.post(
        url=f'{fullhost}/foundry-catalog/api/catalog/datasets/{dataset_rid}/transactions',
        headers=headers,
        data=f'{{"branchId": "{branch_id}"}}'
    )
    tx_rid = response.json()['rid']
    
    # Set transaction type (delete)
    response = requests.post(
        url=f'{fullhost}/foundry-catalog/api/catalog/datasets/{dataset_rid}/transactions/{tx_rid}',
        headers=headers,
        data='"DELETE"'
    )
    
    # Specify the file to be deleted
    response = requests.post(
        url=f'{fullhost}/foundry-catalog/api/catalog/datasets/{dataset_rid}/transactions/{tx_rid}/files/addToDeleteTransaction',
        headers=headers,
        data=f'{{"logicalPaths": ["{file}"]}}'
    )
    
    # Commit transaction
    response = requests.post(
        url=f'{fullhost}/foundry-catalog/api/catalog/datasets/{dataset_rid}/transactions/{tx_rid}/commit',
        headers=headers,
        data='{}'
    )
    

    Result:

    resulting dataset view

    DELETE transaction then can be seen in the History tab:

    DELETE transaction in the History tab