Delete a record
Permanently remove a record by its sys_id.
When to use this pattern
- You're cleaning up test or imported data
- You're implementing retention rules for records your integration owns
- You're sure you want a hard delete — deletion is permanent, and cascade rules on the table may delete related records too
Required values
| Value | Description |
|---|---|
| Table name | The record's table (for example, incident) |
| Record sys_id | The record to delete |
Example
package main
import (
"context"
"fmt"
"log"
servicenow "github.com/michaeldcanady/servicenow-sdk-go/v2"
"github.com/michaeldcanady/servicenow-sdk-go/v2/credentials"
)
func main() {
// Step 1: Authenticate and initialize the client
cred := credentials.NewBasicProvider("{username}", "{password}")
client, err := servicenow.NewServiceNowServiceClient(
servicenow.WithAuthenticationProvider(cred),
servicenow.WithInstance("{instance}"),
)
if err != nil {
log.Fatalf("failed to initialize client: %v", err)
}
// Step 2: Delete the record by sys_id — this is permanent
sysID := "{SysID}"
if err := client.Now().Table("{TableName}").ByID(sysID).Delete(context.Background(), nil); err != nil {
log.Fatalf("unable to delete record: %v", err)
}
fmt.Printf("Deleted record %s\n", sysID)
}
Tips
Deletereturns only anerror— no response body.nilmeans the record is gone.- Deleting an already-deleted record returns
core.NotFoundError; decide whether that's a failure for your workflow or an "already done." - Prefer deactivating (
active=falsevia update) when other records may reference this one.
Next steps
- Delete aging attachments: The same retention pattern, applied to files.