How to Delete Records in Odoo Database Using External API

Abid Patel
06-Nov-2024 Updated : 06-Nov-2024

Learn how to delete records from the Odoo database using the External API with the unlink method. Step-by-step guide for seamless remote record management.

How to Delete Records in Odoo Database Using External API

Deleting data in Odoo with External API is simple. This can be beneficial. Especially when integrating Odoo with other apps. These apps require remote records management. You can commonly do this with Odoo's unlink method. This is accessed through XML-RPC or JSON-RPC protocols. In this text we will guide you. The guide is on how to delete records. It is safe. It is done efficiently.

Why Use External API to Delete Records in Odoo?

Use External API for record deletion. It allows for integration with other applications. Cleanup tasks can automate. To manage data remotely, one does not need direct access to Odoo. For instance, you may need to delete outdated sales orders. Or you may want to erase customer records from an external application.

Deleting Records with unlink

unlink method is Odoo’s standard for record deletion. While using External API you will call this method. You'll specify the IDs of the records you wish to remove.

Illustration: Erasing Records via XML-RPC

Presented below is a Python sample. It illustrates how to delete records in the Odoo database with XML-RPC. Let's suggest that we desire to delete certain customer records. These records are in the model named res.partner.

python

import xmlrpc.client

# Connection details
url = "https://your-odoo-server.com"
db = "your_database_name"
username = "your_username"
password = "your_password"

# Connect to the Odoo server
common = xmlrpc.client.ServerProxy(f"{url}/xmlrpc/2/common")
uid = common.authenticate(db, username, password, {})

# Create a connection to the object API
models = xmlrpc.client.ServerProxy(f"{url}/xmlrpc/2/object")

# Define the record IDs you want to delete (e.g., customer IDs)
record_ids = [1, 2, 3]

# Perform the delete operation using unlink
result = models.execute_kw(db, uid, password, 'res.partner', 'unlink', [record_ids])

if result:
    print("Records deleted successfully.")
else:
    print("Failed to delete records.")

In this example:

  • ▹ url denotes base URL for your Odoo server.
  • ▹ db specifies database name.
  • ▹ record_ids represents List of IDs. You'll add the records you wish to delete to this list.
  • ▹ res.partner is model name. But you can replace it with model of your choice too.

This script interfaces with server. It then authenticates user. Following this it triggers unlink method. Running on specified records in model. If the unlink method calls successfully, it deletes records.

Explaining the Code

  • ▹ Authentication - script authenticates first. It does this in order to obtain uid. This uid is necessary for further API calls.
  • ▹ Model Selection - sets up model API connection for res.partner. This allows for operations on customer records.
  • Unlink Operation - finally unlink is called with list of record IDs. If operation succeeds specified records are deleted.

Important Considerations

  • ▹ Permissions: User should have enough permissions. This is important for deleting records in target model. Without proper access rights delete operation will not succeed.
  • ▹ Data Integrity: Deleting records directly could cause data integrity problems. This is especially true if other records depend on them. Be careful to avoid breaking relationships.
  • ▹ Testing: API call needs testing. It should be a small reversible action. E.g., deleting non-essential test data. This is to verify it works correctly before applying it to production data.

Conclusion

Using Odoo’s External API is beneficial. It is used to delete records. This tool is helpful in maintaining data accuracy. It is also good for integrating with external applications. The unlink method is provided. It offers a simple way to manage records remotely. However one should be cautious. The goal is to ensure you don’t inadvertently delete critical data.

Make a Comment

Your email address will not be published. Required fields are marked *