{
  "schema_version": 2,
  "id": "operate/oss_and_stack/stack-with-enterprise/deprecated-features/graph/graph-quickstart",
  "title": "Graph quick start",
  "url": "https://redis.io/docs/latest/operate/oss_and_stack/stack-with-enterprise/deprecated-features/graph/graph-quickstart/",
  "summary": "Graph quick start",
  "tags": [
    "docs",
    "operate",
    "stack"
  ],
  "last_updated": "2026-10-06T08:53:14-07:00",
  "page_type": "content",
  "content_hash": "1e8c54f15b57f318ca1fd190fa2860c1b8b174aee764ed2bb07e6fb2e63413b8",
  "sections": [
    {
      "id": "prerequisites",
      "title": "Prerequisites",
      "role": "overview",
      "text": "For this quick start tutorial, you need:\n\n- A Redis database with the RedisGraph module enabled. You can use either:\n\n    - A [Redis Cloud](https://redis.io/docs/latest/operate/oss_and_stack/stack-with-enterprise/stack-quickstart) database\n\n    - A [Redis Software](https://redis.io/docs/latest/operate/rs/installing-upgrading/modules/add-module-to-database) database\n\n- [`redis-cli`](https://redis.io/docs/latest/operate/rs/references/cli-utilities/redis-cli) command-line tool\n\n- [`redis-py`](https://github.com/redis/redis-py) client library v4.1.0 or later"
    },
    {
      "id": "graph-with-redis-cli",
      "title": "Graph with `redis-cli`",
      "role": "content",
      "text": "To begin, [connect to your database](https://redis.io/docs/latest/operate/rs/references/cli-utilities/redis-cli#connect-to-a-database) with `redis-cli`."
    },
    {
      "id": "create-a-graph",
      "title": "Create a graph",
      "role": "content",
      "text": "When you create a graph, you can define nodes and the relationships between them with this format:\n\n`(:<node 1>)-[:<relationship>]->(:<node 2>)`\n\nTo define multiple nodes and relationships in a single creation query, separate the entries with commas.\n\nFor example, use the [`CREATE`](https://github.com/RedisGraph/RedisGraph/blob/master/docs/commands/graph.query.md#create) query to create a new graph of motorcycle riders and teams participating in the MotoGP league:\n\n[code example]"
    },
    {
      "id": "add-nodes",
      "title": "Add nodes",
      "role": "content",
      "text": "You can add new nodes to a previously created graph:\n\n[code example]"
    },
    {
      "id": "add-relationships",
      "title": "Add relationships",
      "role": "content",
      "text": "To create new relationships between nodes of a graph:\n\n[code example]"
    },
    {
      "id": "query-the-graph",
      "title": "Query the graph",
      "role": "content",
      "text": "After you create a graph, you can use the [`GRAPH.QUERY`](https://github.com/RedisGraph/RedisGraph/blob/master/docs/commands/graph.query.md) command to query the graph's data.\n\nThe following example returns which motorcycle riders compete for team Yamaha:\n\n[code example]\n\nYou can also use [functions](https://github.com/RedisGraph/RedisGraph/blob/master/docs/commands/graph.query.md#functions) to create more complex queries.\n\nFor example, you can use the `count` function to check how many riders represent team Honda:\n\n[code example]"
    },
    {
      "id": "delete-nodes",
      "title": "Delete nodes",
      "role": "content",
      "text": "You can use the [`DELETE`](https://github.com/RedisGraph/RedisGraph/blob/master/docs/commands/graph.query.md#delete) query to remove a specific node and its relationships from the graph:\n\n[code example]"
    },
    {
      "id": "delete-relationships",
      "title": "Delete relationships",
      "role": "content",
      "text": "You can also use the [`DELETE`](https://github.com/RedisGraph/RedisGraph/blob/master/docs/commands/graph.query.md#delete) query to delete a node's relationships without removing any nodes:\n\n[code example]"
    },
    {
      "id": "delete-a-graph",
      "title": "Delete a graph",
      "role": "content",
      "text": "To delete an entire graph, including all nodes and relationships, run the [`GRAPH.DELETE`](https://github.com/RedisGraph/RedisGraph/blob/master/docs/commands/graph.delete.md) command:\n\n[code example]"
    },
    {
      "id": "graph-with-python",
      "title": "Graph with Python",
      "role": "content",
      "text": "If you want to use graphs within an application, you can use one of these [client libraries](https://github.com/RedisGraph/RedisGraph/blob/master/docs/docs/clients.md).\n\nThe following example uses the Redis Python client library [redis-py](https://github.com/redis/redis-py), which supports graph commands as of v4.1.0.\n\nThis Python code creates a graph that represents friendships between users on a social media website. It also shows how to run queries and change relationships between users.\n\n[code example]\n\nExample output:\n\n[code example]"
    },
    {
      "id": "visualize-graphs-with-redis-insight",
      "title": "Visualize graphs with Redis Insight",
      "role": "content",
      "text": "You can use the [Redis Insight](https://redis.io/docs/latest/develop/tools/insight) workbench to visualize the relationships between the nodes of your graph.\n\n1. Connect to your database with Redis Insight. You can [connect manually](https://redis.io/docs/latest/develop/tools/insight#add-a-standalone-redis-database) or use the [auto-discovery](https://redis.io/docs/latest/develop/tools/insight#auto-discovery-for-redis-cloud-databases) feature.\n\n1. Select the **Workbench** button.\n\n1. Enter a graph query in the text editor.\n\n   For example, this query returns all nodes and relationships in the graph:\n\n   [code example]\n\n1. Select **Run** to run the query.\n\nAfter you run a query, the output log displays a visual representation of your graph's nodes and relationships:\n\n![Visualize a graph with Redis Insight workbench.](https://redis.io/docs/latest/images/modules/visualize-graph.png)"
    },
    {
      "id": "more-info",
      "title": "More info",
      "role": "content",
      "text": "- [Graph commands](https://redis.io/docs/latest/operate/oss_and_stack/stack-with-enterprise/deprecated-features/graph/commands)\n- [RedisGraph client libraries](https://github.com/RedisGraph/RedisGraph/blob/master/docs/docs/clients.md)\n- [RedisGraph documentation on GitHub](https://github.com/RedisGraph/RedisGraph/tree/master/docs/docs)"
    }
  ],
  "examples": [
    {
      "id": "create-a-graph-ex0",
      "language": "sh",
      "code": "127.0.0.1:12543> GRAPH.QUERY MotoGP \"CREATE (:Rider {name:'Valentino Rossi'})-[:rides]->(:Team {name:'Yamaha'}), (:Rider {name:'Dani Pedrosa'})-[:rides]->(:Team {name:'Honda'}), (:Rider {name:'Andrea Dovizioso'})-[:rides]->(:Team {name:'Ducati'})\"\n1) 1) \"Labels added: 2\"\n   2) \"Nodes created: 6\"\n   3) \"Properties set: 6\"\n   4) \"Relationships created: 3\"\n   5) \"Cached execution: 0\"\n   6) \"Query internal execution time: 0.385472 milliseconds\"",
      "section_id": "create-a-graph"
    },
    {
      "id": "add-nodes-ex0",
      "language": "sh",
      "code": "127.0.0.1:12543> GRAPH.QUERY MotoGP \"CREATE (:Rider {name:'Jorge Lorenzo'})\"\n1) 1) \"Nodes created: 1\"\n   2) \"Properties set: 1\"\n   3) \"Cached execution: 0\"\n   4) \"Query internal execution time: 0.185841 milliseconds\"",
      "section_id": "add-nodes"
    },
    {
      "id": "add-relationships-ex0",
      "language": "sh",
      "code": "127.0.0.1:12543> GRAPH.QUERY MotoGP \"MATCH (r:Rider), (t:Team) WHERE r.name = 'Jorge Lorenzo' and t.name = 'Honda' CREATE (r)-[:rides]->(t)\"\n1) 1) \"Relationships created: 1\"\n   2) \"Cached execution: 0\"\n   3) \"Query internal execution time: 0.356578 milliseconds\"",
      "section_id": "add-relationships"
    },
    {
      "id": "query-the-graph-ex0",
      "language": "sh",
      "code": "127.0.0.1:12543> GRAPH.QUERY MotoGP \"MATCH (r:Rider)-[:rides]->(t:Team) WHERE t.name = 'Yamaha' RETURN r,t\"\n1) 1) \"r\"\n   2) \"t\"\n2) 1) 1) 1) 1) \"id\"\n            2) \"0\"\n         2) 1) \"labels\"\n            2) 1) \"Rider\"\n         3) 1) \"properties\"\n            2) 1) 1) \"name\"\n                  2) \"Valentino Rossi\"\n      2) 1) 1) \"id\"\n            2) \"1\"\n         2) 1) \"labels\"\n            2) 1) \"Team\"\n         3) 1) \"properties\"\n            2) 1) 1) \"name\"\n                  2) \"Yamaha\"\n3) 1) \"Cached execution: 0\"\n   2) \"Query internal execution time: 0.500535 milliseconds\"",
      "section_id": "query-the-graph"
    },
    {
      "id": "query-the-graph-ex1",
      "language": "sh",
      "code": "127.0.0.1:12543> GRAPH.QUERY MotoGP \"MATCH (r:Rider)-[:rides]->(t:Team {name:'Honda'}) RETURN count(r)\"\n1) 1) \"count(r)\"\n2) 1) 1) \"2\"\n3) 1) \"Cached execution: 0\"\n   2) \"Query internal execution time: 0.445760 milliseconds\"",
      "section_id": "query-the-graph"
    },
    {
      "id": "delete-nodes-ex0",
      "language": "sh",
      "code": "127.0.0.1:12543> GRAPH.QUERY MotoGP \"MATCH (r:Rider {name: 'Dani Pedrosa'}) DELETE r\"\n1) 1) \"Nodes deleted: 1\"\n   2) \"Relationships deleted: 1\"\n   3) \"Cached execution: 0\"\n   4) \"Query internal execution time: 0.276815 milliseconds\"",
      "section_id": "delete-nodes"
    },
    {
      "id": "delete-relationships-ex0",
      "language": "sh",
      "code": "127.0.0.1:12543> GRAPH.QUERY MotoGP \"MATCH (:Rider {name: 'Valentino Rossi'})-[r:rides]->() DELETE r\"\n1) 1) \"Relationships deleted: 1\"\n   2) \"Cached execution: 0\"\n   3) \"Query internal execution time: 0.348346 milliseconds\"",
      "section_id": "delete-relationships"
    },
    {
      "id": "delete-a-graph-ex0",
      "language": "sh",
      "code": "127.0.0.1:12543> GRAPH.DELETE MotoGP\n\"Graph removed, internal execution time: 0.013138 milliseconds\"",
      "section_id": "delete-a-graph"
    },
    {
      "id": "graph-with-python-ex0",
      "language": "python",
      "code": "import redis\nfrom redis.commands.graph.edge import Edge\nfrom redis.commands.graph.node import Node\n\n# Connect to a database\nr = redis.Redis(host=\"<endpoint>\", port=\"<port>\", \n                password=\"<password>\")\n\n# Create nodes that represent users\nusers = { \"Alex\": Node(label=\"Person\", properties={\"name\": \"Alex\", \"age\": 35}),\n          \"Jun\": Node(label=\"Person\", properties={\"name\": \"Jun\", \"age\": 33}),\n          \"Taylor\": Node(label=\"Person\", properties={\"name\": \"Taylor\", \"age\": 28}),\n          \"Noor\": Node(label=\"Person\", properties={\"name\": \"Noor\", \"age\": 41}) }\n\n# Define a graph called SocialMedia\nsocial_graph = r.graph(\"SocialMedia\")\n\n# Add users to the graph as nodes\nfor key in users.keys():\n    social_graph.add_node(users[key])\n\n# Add relationships between user nodes\nsocial_graph.add_edge( Edge(users[\"Alex\"], \"friends\", users[\"Jun\"]) )\n# Make the relationship bidirectional\nsocial_graph.add_edge( Edge(users[\"Jun\"], \"friends\", users[\"Alex\"]) )\n\nsocial_graph.add_edge( Edge(users[\"Jun\"], \"friends\", users[\"Taylor\"]) )\nsocial_graph.add_edge( Edge(users[\"Taylor\"], \"friends\", users[\"Jun\"]) )\n\nsocial_graph.add_edge( Edge(users[\"Jun\"], \"friends\", users[\"Noor\"]) )\nsocial_graph.add_edge( Edge(users[\"Noor\"], \"friends\", users[\"Jun\"]) )\n\nsocial_graph.add_edge( Edge(users[\"Alex\"], \"friends\", users[\"Noor\"]) )\nsocial_graph.add_edge( Edge(users[\"Noor\"], \"friends\", users[\"Alex\"]) )\n\n# Create the graph in the database\nsocial_graph.commit()\n\n# Query the graph to find out how many friends Alex has\nresult1 = social_graph.query(\"MATCH (p1:Person {name: 'Alex'})-[:friends]->(p2:Person) RETURN count(p2)\")\nprint(\"Alex's original friend count:\", result1.result_set)\n\n# Delete a relationship without deleting any user nodes\nsocial_graph.query(\"MATCH (:Person {name: 'Alex'})<-[f:friends]->(:Person {name: 'Jun'}) DELETE f\")\n\n# Query the graph again to see Alex's updated friend count\nresult2 = social_graph.query(\"MATCH (p1:Person {name: 'Alex'})-[:friends]->(p2:Person) RETURN count(p2)\")\nprint(\"Alex's updated friend count:\", result2.result_set)\n\n# Delete the entire graph\nsocial_graph.delete()",
      "section_id": "graph-with-python"
    },
    {
      "id": "graph-with-python-ex1",
      "language": "text",
      "code": "$ ./quick_start.py\nAlex's original friend count: [[2]]\nAlex's updated friend count: [[1]]",
      "section_id": "graph-with-python"
    },
    {
      "id": "visualize-graphs-with-redis-insight-ex0",
      "language": "sh",
      "code": "GRAPH.QUERY MotoGP \"MATCH (n) RETURN n\"",
      "section_id": "visualize-graphs-with-redis-insight"
    }
  ]
}
