Deleting Data

The easiest way to clean up data is by deleting it through the API. There are three ways to delete objects through the Faros revisions API:

  1. Delete by key: This will delete only the entry that matches the provided key for a given model type.
  2. Delete node by id: This will delete only the entity that matches the node ID for a given model type.
  3. Delete by origin: This will delete ⚠️ALL records⚠️ of a given type that were written by the provided origin. Origin is the ID for the source, UI page, or API command that created the objects.

You’ll first need to construct a GraphQL query to determine the origin or specific key(s) you wish to delete. You’ll then create your delete entries and post them to the revisions api. Specific examples can be found below.

Example: Deleting Phantoms

Delete Team Membership Phantoms by key

  1. Query all the Team Membership entries and filter to those that refer to either a phantom team or a phantom employee.

Query:

query {  
  org {  
    teamMemberships {  
      nodes {  
        team {  
          uid  
          metadata { isPhantom }  
        }  
        member {  
          uid  
          metadata { isPhantom }  
        }  
      }  
    }  
  }  
}

JSONata:
data.org.teamMemberships.nodes[team.metadata.isPhantom or member.metadata.isPhantom]

  1. Note their keys
    Team membership keys are in the form
    {"team":{"uid": <team_uid>}, "member":{"uid":<employee_uid>}}

  2. Construct a deletion entry per key

"org_TeamMembership____Deletion": { "where": {"team":{"uid": "my_team"}, "member":{"uid":"employee1"}} } }

  1. Send a Post request to the /revisions endpoint with your deletion entries
curl -X 'POST'  
  'https://prod.api.faros.ai/graphs/<YOUR_GRAPH>/revisions'  
  -H 'accept: application/json'  
  -H 'authorization: <YOUR_AUTH>'  
  -H 'Content-Type: application/json'  
  -d '{  
  "origin": "manual-deletion",  
  "entries": [  
    { 
      "org_TeamMembership__Deletion":  
      {  
        "where": {  
          "team": { "uid": "my_deleted_team" },  
          "Member": { "uid":"employee1" }  
        }  
      }  
    },  
    { 
      "org_TeamMembership__Deletion":  
      {  
        "where": {  
          "team": { "uid": "my_deleted_team" },  
          "Member": { "uid": "employee2" }  
        }  
      }  
    },  
    { 
      "org_TeamMembership__Deletion":  
      {  
        "where": {  
          "team": { "uid": "team1" },  
          "Member": { "uid": "deleted_employee" }  
        }  
      }  
    }  
  ]  
}'

Delete by node id

  1. Query the node you are looking for in GraphQL. All entities in the Faros graphs are called nodes in GraphQL and all have an ID field (note: this is different from the uid field)
    The query below contains other fields so we can tell which node ID is the one we wish to delete.
{  
  org {  
    boardOwnerships {  
      nodes {  
        id  
        board { uid }  
        team { uid }  
      }  
    }  
  }  
}
  1. Construct a deletion entry for the node ID and model type you wish to delete
    { "<modelName>__Deletion": { "where": { "id": <node_id> } } }
  2. Send a Post request to the revisions endpoint with your deletion entry.
    Note: Make sure you have the schema type set to v2
curl -X 'POST'  
  'https://prod.api.faros.ai/graphs/<YOUR_GRAPH>/revisions'  
  -H 'accept: application/json'  
  -H 'x-faros-revision-entries-version: v2'  
  -H 'authorization: <YOUR_AUTH>'  
  -H 'Content-Type: application/json'  
  -d '{  
  "origin": "manual-deletion",  
  "entries": [  
    {  
      "org_RepositoryOwnership__Deletion": {  
        "where": { "id": <node_id> }  
      }  
    }  
  ]  
}'

Delete by Origin

Query the models you are looking at in GraphQL to understand their origins. Origins are metadata explaining where that entry came from. In general this is the name of the source that wrote the entry, or, if the entry did not come from a source, the name of the page or process responsible.

{  
  org {  
    boardOwnerships {  
      nodes {  
        metadata { origin } 
        board { uid }  
        team { uid }  
      }  
    }  
  }  
}
  1. Construct a deletion entry for the node ID and model type you wish to delete
    { "<modelName>__Deletion": { "where": <origin> } }
  2. Send a Post request to the revisions endpoint with your deletion request:
curl -X 'POST'  
  'https://prod.api.faros.ai/graphs/<YOUR_GRAPH>/revisions'  
  -H 'accept: application/json'  
  -H 'x-faros-revision-entries-version: v2'  
  -H 'authorization: <YOUR_AUTH>'  
  -H 'Content-Type: application/json'  
  -d '{  
  "origin": "manual-deletion",  
  "entries": [  
    {  
      "org_RepositoryOwnership__Deletion": {  
        "where": <origin>  
      }  
    }  
  ]  
}'

If you would like to delete ALL data written from a specific origin use the following script:

./clean_source_data.sh https://prod.api.faros.ai <YOUR_AUTH> <YOUR_GRAPH> <origin>

⚠️

Deletes ALL of the entries written by the origin

#!/usr/bin/env bash

# Delete all data in a graph for a source by origin:

FAROS_API_URL=$1
if [ -z "$FAROS_API_URL" ]; then
    echo "FAROS_API_URL not provided"
    exit 1
fi

FAROS_API_KEY=$2
if [ -z "$FAROS_API_KEY" ]; then
    echo "FAROS_API_KEY not provided"
    exit 1
fi

GRAPH_NAME=$3
if [ -z "$GRAPH_NAME" ]; then
    echo "GRAPH_NAME not provided"
    exit 1
fi

ORIGIN_NAME=$4
if [ -z "$ORIGIN_NAME" ]; then
    echo "ORIGIN_NAME not provided"
    exit 1
fi

# Get all the models in the graph
MODELS=$(curl "$FAROS_API_URL/graphs/$GRAPH_NAME/models" \
  -X GET \
  -H "Authorization: $FAROS_API_KEY" | \
  jq -r '.models | map ({(.name + "__Deletion"): {"where": "'"$ORIGIN_NAME"'"}})'
)

# Open a new revision on the revision deleting all data
DATA='{"origin": "'"$ORIGIN_NAME"'", "entries": '$MODELS'}'
curl "$FAROS_API_URL/graphs/$GRAPH_NAME/revisions" \
  -X POST \
  -H "Authorization: $FAROS_API_KEY" \
  -H "Content-Type: application/json" \
  -d "$DATA" || exit 1

# Open a new revision on the revision to clear the state
REVISION_ID=$(curl "$FAROS_API_URL/graphs/$GRAPH_NAME/revisions" \
  -X POST \
  -H "Authorization: $FAROS_API_KEY" \
  -H 'Content-Type: application/json' \
  -d '{
        "origin": "'"$ORIGIN_NAME"'",
        "lock": { "name": "'"$ORIGIN_NAME"'" }
      }' | \
  jq -r '.revision.uid'
)
echo -e "Opened revision $REVISION_ID to clear state"

# Close revision and clear state
curl "$FAROS_API_URL/graphs/$GRAPH_NAME/revisions/$REVISION_ID" \
  -X PATCH \
  -H "Authorization: $FAROS_API_KEY" \
  -H 'Content-Type: application/merge-patch+json' \
  -d '{
        "status": "active",
        "lock": {
            "state": null
        }
     }'

echo -e "\n\nDone initializing state for origin $ORIGIN_NAME"