CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-grafana-backup

A Python-based application to backup Grafana settings using the Grafana API

Pending
Overview
Eval results
Files

delete-operations.mddocs/

Delete Operations

Selective deletion functionality for Grafana components. The delete system provides safe removal of Grafana objects with component-specific deletion strategies and safety measures to prevent accidental data loss.

Capabilities

Main Delete Function

The primary delete entry point that orchestrates component deletion with API validation and selective component processing.

def main(args, settings):
    """
    Main delete function that handles selective component deletion
    
    Args:
        args (dict): Command line arguments including:
            - '--components': Comma-separated list of components to delete
        settings (dict): Configuration settings loaded from grafanaSettings
    """

Supported Delete Components

The delete system supports a subset of components with safety considerations:

Dashboard Deletion

def main(args, settings):
    """
    Delete all dashboards from Grafana instance
    
    Module: grafana_backup.delete_dashboards
    Features: UID-based deletion, folder handling, bulk operations
    Safety: Confirmation prompts, selective deletion by folder
    """

Datasource Deletion

def main(args, settings):
    """
    Delete all datasources from Grafana instance
    
    Module: grafana_backup.delete_datasources
    Features: UID-based deletion, dependency checking
    Safety: Validates no dashboards are using datasources before deletion
    """

Folder Deletion

def main(args, settings):
    """
    Delete all folders from Grafana instance
    
    Module: grafana_backup.delete_folders
    Features: Recursive deletion, dashboard handling
    Safety: Moves dashboards to General folder before folder deletion
    """

Alert Channel Deletion

def main(args, settings):
    """
    Delete all alert notification channels
    
    Module: grafana_backup.delete_alert_channels
    Features: Channel type handling, bulk deletion
    Safety: Validates no active alerts are using channels
    """

Snapshot Deletion

def main(args, settings):
    """
    Delete all dashboard snapshots
    
    Module: grafana_backup.delete_snapshots
    Features: Bulk snapshot removal, key-based deletion
    Safety: Snapshot deletion is generally safe as they're copies
    """

Annotation Deletion

def main(args, settings):
    """
    Delete all annotations from dashboards and global scope
    
    Module: grafana_backup.delete_annotations
    Features: Time-range deletion, tag-based filtering
    Safety: Preserves system annotations, focuses on user annotations
    """

Library Element Deletion

def main(args, settings):
    """
    Delete all library elements (reusable panels and variables)
    
    Module: grafana_backup.delete_library_elements
    Features: Element type handling, dependency checking
    Safety: Validates no dashboards are using elements before deletion
    """

Team Member Deletion

def main(args, settings):
    """
    Remove all team members from all teams
    
    Module: grafana_backup.delete_team_members
    Features: Preserves team structure, removes only memberships
    Safety: Teams themselves are preserved to maintain references
    """

Excluded Components

Several component types are intentionally excluded from deletion operations for safety:

Teams Not Deleted

Teams themselves are not deleted because they don't have consistent unique identifiers across Grafana instances. Deleting and recreating teams would break:

  • Folder permission references
  • Team member associations
  • Historical audit trails

Organizations Not Deleted

Organization deletion is not supported to prevent:

  • Complete data loss scenarios
  • Multi-tenant configuration destruction
  • License and user limit complications

Users Not Deleted

User deletion is not supported to preserve:

  • Historical audit trails
  • Dashboard ownership references
  • Permission and role assignments

Usage Examples

Delete All Dashboards

from grafana_backup.delete import main as delete_components
from grafana_backup.grafanaSettings import main as load_config

# Load configuration
settings = load_config('/path/to/grafanaSettings.json')

# Delete all dashboards
args = {
    'delete': True,
    '--components': 'dashboards',
    '--config': None
}

delete_components(args, settings)

Delete Multiple Component Types

# Delete dashboards, datasources, and folders
args = {
    'delete': True,
    '--components': 'dashboards,datasources,folders',
    '--config': None
}

delete_components(args, settings)

Delete All Supported Components

# Delete all components that support deletion
args = {
    'delete': True,
    '--components': None,  # None means all supported components
    '--config': None
}

delete_components(args, settings)

Component-Specific Deletion Details

Dashboard Deletion Strategy

The dashboard deletion process:

  1. Retrieves all dashboards using search API
  2. Handles both ID-based and UID-based deletion depending on Grafana version
  3. Processes dashboards in batches for performance
  4. Preserves folder structure during deletion

Datasource Deletion Strategy

The datasource deletion process:

  1. Lists all datasources in the organization
  2. Checks for dashboard dependencies (optional safety check)
  3. Uses UID-based deletion when supported
  4. Handles built-in datasources appropriately

Folder Deletion Strategy

The folder deletion process:

  1. Moves all dashboards from folders to General folder
  2. Updates folder permissions and references
  3. Deletes folders in reverse hierarchy order
  4. Preserves General folder (cannot be deleted)

Library Element Deletion Strategy

The library element deletion process:

  1. Retrieves all library elements by type
  2. Checks for dashboard usage dependencies
  3. Deletes elements that are not in use
  4. Provides warnings for elements still in use

Safety Features

API Validation

All delete operations include comprehensive API validation:

  • Grafana connectivity and authentication verification
  • Feature support detection (UID, paging, etc.)
  • Permission validation for delete operations

Dependency Checking

Where applicable, the system checks for dependencies:

  • Dashboards using datasources before datasource deletion
  • Dashboards using library elements before element deletion
  • Team references before team member removal

Confirmation and Logging

Delete operations include extensive logging:

  • Pre-deletion validation results
  • Individual item deletion status
  • Error handling and recovery information
  • Summary of deletion operations

Graceful Error Handling

The delete system handles errors gracefully:

  • Continues with remaining items if individual deletions fail
  • Provides detailed error messages for troubleshooting
  • Preserves system stability during bulk operations
  • Exits cleanly on API connectivity issues

Best Practices

Backup Before Deletion

Always create a backup before performing delete operations:

# Create backup first
grafana-backup save

# Then perform deletion
grafana-backup delete --components=dashboards

Selective Deletion

Use selective deletion rather than bulk deletion when possible:

# Delete specific components only
grafana-backup delete --components=snapshots,annotations

Test with Non-Production

Test deletion operations in development environments before production use:

  • Verify component selection works as expected
  • Validate safety checks are appropriate
  • Confirm restoration process if needed

Monitor Dependencies

Be aware of component dependencies:

  • Dashboards depend on datasources and library elements
  • Folders contain dashboards and have permissions
  • Teams have members and folder permissions
  • Alert channels may be used by dashboard alerts

The delete system is designed to be safe but irreversible, so careful planning and testing are essential.

Install with Tessl CLI

npx tessl i tessl/pypi-grafana-backup

docs

admin-tools.md

api-health.md

archive-management.md

backup-operations.md

cloud-storage.md

configuration.md

delete-operations.md

index.md

monitoring.md

restore-operations.md

tile.json