Delete Firewall Rule
Delete Firewall Rule
Section titled “Delete Firewall Rule”Permanently deletes a firewall rule. This action cannot be undone.
Endpoint
Section titled “Endpoint”DELETE /v1/firewall-rules/{id}Path Parameters
Section titled “Path Parameters”| Parameter | Type | Required | Description |
|---|---|---|---|
id | integer | Yes | The firewall rule ID |
Examples
Section titled “Examples”import Tokenrouter from 'tokenrouter';
const client = new Tokenrouter({ apiKey: process.env.TOKENROUTER_API_KEY});
await client.firewallRules.delete(123);
console.log('Rule deleted successfully');from tokenrouter import Tokenrouter
client = Tokenrouter( api_key=os.getenv("TOKENROUTER_API_KEY"))
client.firewall_rules.delete(123)
print("Rule deleted successfully")curl -X DELETE https://api.tokenrouter.io/v1/firewall-rules/123 \ -H "Authorization: Bearer tr_..."Response
Section titled “Response”{ "success": true}Error Responses
Section titled “Error Responses”403 Forbidden - Free Plan
Section titled “403 Forbidden - Free Plan”{ "error": { "message": "Firewall rules are not available on the free plan", "type": "authorization_error", "http_status": 403 }}404 Not Found
Section titled “404 Not Found”{ "error": { "message": "Firewall rule not found", "type": "not_found_error", "http_status": 404 }}Safe Deletion Pattern
Section titled “Safe Deletion Pattern”Always verify before deleting:
// Get rule details firstconst rule = await client.firewallRules.retrieve(ruleId);
console.log(`About to delete: ${rule.data.name}`);console.log(`Priority: ${rule.data.priority}`);console.log(`Action: ${rule.data.action}`);console.log(`Pattern: ${rule.data.pattern}`);
// Confirm deletionconst confirmed = confirm('Delete this rule?');
if (confirmed) { await client.firewallRules.delete(ruleId); console.log('Rule deleted');}# Get rule details firstrule = client.firewall_rules.retrieve(rule_id)
print(f"About to delete: {rule.data.name}")print(f"Priority: {rule.data.priority}")print(f"Action: {rule.data.action}")print(f"Pattern: {rule.data.pattern}")
# Confirm deletionconfirmed = input("Delete this rule? (y/n): ").lower() == 'y'
if confirmed: client.firewall_rules.delete(rule_id) print("Rule deleted")Bulk Deletion
Section titled “Bulk Deletion”Delete multiple rules:
// Delete all disabled rulesconst rules = await client.firewallRules.list();
const disabledRules = rules.data.filter(r => !r.is_enabled);
console.log(`Found ${disabledRules.length} disabled rules to delete`);
for (const rule of disabledRules) { await client.firewallRules.delete(rule.id); console.log(`Deleted: ${rule.name}`);}# Delete all disabled rulesrules = client.firewall_rules.list()
disabled_rules = [r for r in rules.data if not r.is_enabled]
print(f"Found {len(disabled_rules)} disabled rules to delete")
for rule in disabled_rules: client.firewall_rules.delete(rule.id) print(f"Deleted: {rule.name}")Delete with Backup
Section titled “Delete with Backup”Save rule configuration before deleting:
import fs from 'fs';
// Backup rule before deletingconst rule = await client.firewallRules.retrieve(ruleId);
const backup = { name: rule.data.name, priority: rule.data.priority, is_enabled: rule.data.is_enabled, scope: rule.data.scope, type: rule.data.type, pattern: rule.data.pattern, action: rule.data.action, replacement: rule.data.replacement, deleted_at: new Date().toISOString()};
// Save backup to filefs.appendFileSync( 'firewall-deletions.json', JSON.stringify(backup) + '\n');
// Deleteawait client.firewallRules.delete(ruleId);console.log('Rule deleted and backed up');import jsonfrom datetime import datetime
# Backup rule before deletingrule = client.firewall_rules.retrieve(rule_id)
backup = { "name": rule.data.name, "priority": rule.data.priority, "is_enabled": rule.data.is_enabled, "scope": rule.data.scope, "type": rule.data.type, "pattern": rule.data.pattern, "action": rule.data.action, "replacement": rule.data.replacement, "deleted_at": datetime.now().isoformat()}
# Save backup to filewith open("firewall-deletions.json", "a") as f: f.write(json.dumps(backup) + "\n")
# Deleteclient.firewall_rules.delete(rule_id)print("Rule deleted and backed up")Alternative: Disable Instead
Section titled “Alternative: Disable Instead”Consider disabling rules instead of deleting:
// Instead of deleting, disable the ruleawait client.firewallRules.update(ruleId, { is_enabled: false});
// Can be re-enabled later if neededawait client.firewallRules.update(ruleId, { is_enabled: true});# Instead of deleting, disable the ruleclient.firewall_rules.update(rule_id, is_enabled=False)
# Can be re-enabled later if neededclient.firewall_rules.update(rule_id, is_enabled=True)When to Delete vs Disable
Section titled “When to Delete vs Disable”| Scenario | Recommendation |
|---|---|
| Testing a rule | Disable it |
| Seasonal pattern (e.g., holiday-specific) | Disable it |
| Temporary relaxation of rules | Disable it |
| Deprecated pattern no longer needed | Delete it |
| Duplicate rule | Delete it |
| Incorrect rule that never worked | Delete it |
| Rule with security vulnerabilities | Delete it |
Cleanup Strategies
Section titled “Cleanup Strategies”Remove Old Unused Rules
Section titled “Remove Old Unused Rules”const rules = await client.firewallRules.list();
const now = new Date();const ninetyDaysAgo = new Date(now.getTime() - 90 * 24 * 60 * 60 * 1000);
for (const rule of rules.data) { const updated = new Date(rule.updated_at);
if (!rule.is_enabled && updated < ninetyDaysAgo) { console.log(`Deleting old rule: ${rule.name} (last updated ${updated.toLocaleDateString()})`); await client.firewallRules.delete(rule.id); }}from datetime import datetime, timedelta
rules = client.firewall_rules.list()
now = datetime.now()ninety_days_ago = now - timedelta(days=90)
for rule in rules.data: updated = datetime.fromisoformat(rule.updated_at.replace('Z', '+00:00'))
if not rule.is_enabled and updated < ninety_days_ago: print(f"Deleting old rule: {rule.name} (last updated {updated.date()})") client.firewall_rules.delete(rule.id)Remove Warn-Only Rules
Section titled “Remove Warn-Only Rules”// Delete all warn-action rules (if moving to logging)const rules = await client.firewallRules.list();
const warnRules = rules.data.filter(r => r.action === 'warn');
console.log(`Removing ${warnRules.length} warn-only rules`);
for (const rule of warnRules) { await client.firewallRules.delete(rule.id);}# Delete all warn-action rules (if moving to logging)rules = client.firewall_rules.list()
warn_rules = [r for r in rules.data if r.action == "warn"]
print(f"Removing {len(warn_rules)} warn-only rules")
for rule in warn_rules: client.firewall_rules.delete(rule.id)Best Practices
Section titled “Best Practices”- Always get rule details before deleting
- Backup critical rules before deletion
- Prefer disabling over deleting when unsure
- Delete in batches carefully to avoid accidents
- Document deletion reasons in change log
- Test after deletion to ensure no unexpected behavior
- Review dependencies - check if other systems reference the rule
Recovery
Section titled “Recovery”If you accidentally deleted a rule and have a backup:
// Recreate from backupconst backup = { name: "Block Credit Cards", priority: 100, is_enabled: true, scope: "prompt", type: "regex", pattern: "\\d{16}", action: "block", replacement: null};
const restored = await client.firewallRules.create(backup);console.log(`Restored rule: ${restored.data.id}`);# Recreate from backupbackup = { "name": "Block Credit Cards", "priority": 100, "is_enabled": True, "scope": "prompt", "type": "regex", "pattern": r"\d{16}", "action": "block", "replacement": None}
restored = client.firewall_rules.create(**backup)print(f"Restored rule: {restored.data.id}")