AI

Weaviate Delete Class

Weaviate is an open-source, GraphQL, and RESTful API-based knowledge graph that allows you to execute the semantic and vector searches. It is an incredible tool for building the machine-learning applications such as recommendation systems.

The most basic building block of the Weaviate database is classes and objects. In the context of Weaviate, a class refers to the schema definition of a given object that you wish to store in the database. To create a class in Weaviate, we use the “create class” operation followed by the class definition and the properties that we want to assign to the object.

This tutorial walks you through creating a new class or deleting an existing class using the Weaviate API endpoints and the Weaviate Python SDK.

Requirements:

To follow along with this post, ensure that you have the following:

Installed Weaviate cluster on your machine. You can use Docker or Kubernetes to set up a local cluster. You can also check the Weaviate cloud to create a cloud-hosted instance of the Weaviate database.

Basic knowledge of JSON, HTTP requests, API endpoint, and Python programming language.

Weaviate Create Class

The first step is to create a class that stores the basic data. For example, we can use the /v1/schema API endpoint and send a POST request with the body that defines the class schema.

An example query is as follows:

$ curl \
    -X POST \
    -H "Content-Type: application/json" \
    -d '{
        "class": "DatabaseServer",
        "description": "Information about a database server",
        "properties": [
            {
            "dataType": [
                "string"
            ],
            "description": "Hostname of the database server",
            "name": "hostname"
            },
            {
            "dataType": [
                "string"
            ],
            "description": "IP address of the database server",
            "name": "ipAddress"
            },
            {
            "dataType": [
                "string"
            ],
            "description": "Type of the database, such as MySQL, PostgreSQL, MongoDB, etc.",
            "name": "dbType"
            },
            {
            "dataType": [
                "int"
            ],
            "description": "Port number of the database server",
            "name": "portNumber"
            }
        ]
    }'
\
    http://localhost:8080/v1/schema

The previous command uses curl followed by the -X POST parameter and the URL to the Weaviate server http://localhost:8080/v1/schema/things/.

This should make a POST request to the Weaviate API, telling it that we want to create a new class.

Next, is the -H parameter which allows us to define the MIME type of the data that we wish to provide in the request body. In this case, we wish to pass the application/JSON data.

Once executed, the previous command creates a new DatabaseServer class with four properties: hostname, ipAddress, dbType, and portNumber. We can use this class to store the information about various database servers.

Weaviate Delete Class

Once you are done with a class, you can delete it by sending a DELETE request to the /v1/schema endpoint as shown in the following syntax:

DELETE v1/schema/{class_name}

This should remove the specified class and all data in the instances from the schema.

Weaviate Delete Class Using API Endpoint

The most common and easiest method of deleting a class is using the API endpoint. For example, to remove the DatabaseServer class that we created in the previous command, we can run the query as follows:

$ curl -X DELETE http://localhost:8080/v1/schema/DatabaseServer

Weaviate Delete Class Using Python SDK

Weaviate also provides various SDKs to interact with the server and perform multiple tasks. In our case, we can use the Weaviate Python SDK to delete a specified class as shown in the following example code:

import weaviate
client = weaviate.Client("http://localhost:8080")
client.schema.delete_class("DatabaseServer")

The previous code should remove the DatabaseServer class and the associated data from the schema.

Conclusion

We learned how we can use the API endpoints and the Python SDK for Weaviate to create and drop an existing class (and associated data) from a given schema.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list