Deleting Objects and Properties from the Database
This guide demonstrates how to delete objects and properties from the PlexosDB database.
Deleting Objects
Section titled “Deleting Objects”When you delete an object, all its associated data (properties, memberships, etc.) are automatically removed due to foreign key constraints with cascade deletion.
from plexosdb import PlexosDBfrom plexosdb.enums import ClassEnum
# Initialize databasedb = PlexosDB()db.create_schema(version=10)
# Add a simple test generator objectdb.add_object( ClassEnum.Generator, name="TestGenerator", description="Example generator", category="Thermal")
# Delete the entire object (removes all properties and memberships)db.delete_object(ClassEnum.Generator, name="TestGenerator")Deleting Properties
Section titled “Deleting Properties”You can delete specific properties from objects without removing the object itself. This provides fine-grained control over data management.
Basic Property Deletion
Section titled “Basic Property Deletion”from plexosdb import PlexosDBfrom plexosdb.enums import ClassEnum
# Initialize database from XML or create newdb = PlexosDB.from_xml("model.xml")
# Add a generator with propertiesdb.add_object(ClassEnum.Generator, "PowerPlant1")db.add_property(ClassEnum.Generator, "PowerPlant1", "Max Capacity", 500.0)db.add_property(ClassEnum.Generator, "PowerPlant1", "Min Stable Level", 100.0)
# Delete a specific propertydb.delete_property(ClassEnum.Generator, "PowerPlant1", property_name="Min Stable Level")
# The object still exists, but only with the "Max Capacity" propertyScenario-Specific Property Deletion
Section titled “Scenario-Specific Property Deletion”When properties have scenario-specific values, you can delete only the property data associated with a particular scenario:
# Add properties with different scenariosdb.add_property(ClassEnum.Generator, "PowerPlant1", "Max Capacity", 500.0) # Base casedb.add_property(ClassEnum.Generator, "PowerPlant1", "Max Capacity", 600.0, scenario="High Demand")db.add_property(ClassEnum.Generator, "PowerPlant1", "Max Capacity", 400.0, scenario="Low Demand")
# Delete only the "High Demand" scenario propertydb.delete_property( ClassEnum.Generator, "PowerPlant1", property_name="Max Capacity", scenario="High Demand")
# The base case and "Low Demand" scenario properties remainAdvanced Property Deletion
Section titled “Advanced Property Deletion”You can specify custom collections and parent classes when deleting properties:
from plexosdb.enums import CollectionEnum
# Delete property with specific collectiondb.delete_property( ClassEnum.Generator, "PowerPlant1", property_name="Heat Rate", collection=CollectionEnum.GeneratorProperties, parent_class=ClassEnum.System)Important Notes
Section titled “Important Notes”Cascade Deletion Behavior
Section titled “Cascade Deletion Behavior”- Object deletion: Removes the object and ALL associated data (properties, memberships, text data, etc.)
- Property deletion: Removes only the specified property data, including associated text, tags, and date ranges
- Scenario-specific deletion: Removes only property data tagged with the specified scenario
Error Handling
Section titled “Error Handling”The deletion methods will raise appropriate exceptions if:
- The object doesn’t exist (
NotFoundError) - The property name is invalid for the collection (
NameError) - The property doesn’t exist for the object (
NotFoundError) - The specified scenario doesn’t exist (
NotFoundError)
from plexosdb.exceptions import NotFoundError, NameError
try: db.delete_property(ClassEnum.Generator, "NonexistentGen", property_name="Max Capacity")except NotFoundError as e: print(f"Error: {e}")
try: db.delete_property(ClassEnum.Generator, "PowerPlant1", property_name="Invalid Property")except NameError as e: print(f"Error: {e}")Best Practices
Section titled “Best Practices”- Backup data: Always backup your database before performing bulk deletions
- Verify existence: Check that objects and properties exist before attempting deletion
- Use transactions: For complex operations, wrap deletions in database transactions
- Scenario management: Be specific about scenarios when deleting scenario-based properties
# Example of safe deletion with verificationif db.check_object_exists(ClassEnum.Generator, "PowerPlant1"): properties = db.get_object_properties(ClassEnum.Generator, "PowerPlant1") property_names = [p["property"] for p in properties]
if "Max Capacity" in property_names: db.delete_property(ClassEnum.Generator, "PowerPlant1", property_name="Max Capacity") print("Property deleted successfully") else: print("Property not found")else: print("Object not found")