Skip to content
Documentation
Updated 17 minutes to read
Markdown

Easily automate the safe removal of historical Nodinite Log Databases that are no longer required by your business. This guide empowers you to:

  • ✅ Maintain compliance and optimize storage with automated cleanup
  • ✅ Reduce manual DBA workload and risk of human error
  • ✅ Follow best practices for data retention and system stability
  • ✅ Use SQL Server Agent jobs for reliable, repeatable maintenance

Nodinite automatically creates new databases when the following System Parameters are met:


It is safe to remove empty old Log Databases. However, even empty historical Log Databases consume disk space and create overhead — CPU, disk I/O, and backup pressure. This process is designed to minimize risk and optimize performance.

Data retention in the Log Databases is governed by System Parameters. Log Events are individually removed based on the Message Type. The following System Parameters specify how long to keep events and data by default:

A Log Database is safe to remove once all its data has been purged according to your retention policy and the EndDate for that database has passed. The scripts below automatically detect such databases — you only need to set the variables.

flowchart TB New["fa:fa-database Online Log Database
created by the Logging Service"] -->|"Size or days reached"| Archive["fa:fa-box-archive Archive Log Database
EndDate set"] Archive -->|"Retention purges Log Events"| Expired["fa:fa-hourglass-end Expired
EndDate older than @days"] Expired -->|"Job step 1: Script A"| Dropped["fa:fa-trash Database dropped
on SQL Server"] Dropped -->|"Job step 2: Script B"| Clean["fa:fa-broom Row removed
from LogTables"] Dropped -.->|"Step 1 fails: job quits"| Stop["fa:fa-ban Step 2 never runs"] style New fill:#2678BD style Archive fill:#3F827B style Expired fill:#FFD525 style Dropped fill:#E47528 style Clean fill:#93d16d style Stop fill:#c33d34

Diagram: the life of a Log Database. The Logging Service creates a new online Log Database when SizeToSplitDatabaseOn or DaysToSplitDatabaseOn is reached; the previous one becomes an archive. Retention System Parameters purge its Log Events, and once its EndDate is older than @days, the SQL Server Agent job on this page drops it (Script A) and then removes its row from the Configuration Database (Script B). Red marks the safety stop when Script A fails.


Make sure you have the following before continuing:

  • ✅ SQL Server Management Studio (SSMS) installed on your computer — Download SSMS free from Microsoft
  • ✅ A SQL Server login with sysadmin privileges on the server that hosts the Nodinite Configuration Database
  • ✅ The SQL Server Agent service is running on that SQL Server instance (check with your DBA if unsure)
  • ✅ The name of your Nodinite Configuration Database — for example, Nodinite_Prod_Config
  • ✅ The prefix of your Nodinite Log Databases — for example, Nodinite_Prod_Log_

Use SQL Server Sysadmin Requirements to align DBA policy decisions with installation, first database creation, and optional maintenance behavior.


The job uses two T-SQL scripts that must run in sequence. Never swap the order — Step 1 removes the physical databases, Step 2 removes the records from Nodinite. If Step 1 fails, Step 2 must never run.

Update the following four variables at the top of both scripts to match your environment before using them in the job:

Variable Description Example
@prefix The prefix portion of your Nodinite Log Database names v7: Nodinite_Prod_Log_
Created by v6: NodiniteLog_Prod_
@configDB The name of the Nodinite Configuration Database v7: Nodinite_Prod_Config
Created by v6: NodiniteConfig_Prod
@days How old (in days) a database must be before it is eligible for removal — must be a positive integer 180
@managedInstance 1 if the Log Databases are hosted on Azure SQL Managed Instance, otherwise 0 — see Azure SQL Managed Instance 0

Before using the scripts, check where your Log Databases physically reside. Run the following query on your Config DB:

SELECT [Database], [Server], [IsRemoteServer] FROM [dbo].[LogTables] ORDER BY [StartDate]
IsRemoteServer Meaning Which script to use
0 The Log Database is on the same SQL Server instance as the Config DB Run the job on the Config DB server — this is the standard case
1 The Log Database is on a different SQL Server instance (connected via a linked server) Run the job on the remote SQL Server that hosts the Log Databases
1 The Log Databases are on Azure SQL Managed Instance Set @managedInstance = 1 and run the job on the Managed Instance — see Azure SQL Managed Instance

On Azure SQL Managed Instance, Log Databases are registered in Nodinite with IsRemoteServer = 1, and Managed Instance does not allow ALTER DATABASE … SET SINGLE_USER. Set @managedInstance = 1 in both scripts to handle this:

Script behavior @managedInstance = 0 (SQL Server) @managedInstance = 1 (Managed Instance)
Selects Log Databases with IsRemoteServer = 0 IsRemoteServer = 1
Disconnects open sessions before the drop Yes — SET SINGLE_USER WITH ROLLBACK IMMEDIATE No — not supported on Managed Instance

This section does not apply to Azure SQL Managed Instance, where IsRemoteServer = 1 is expected. If your Log Databases are visible on the same SQL Server instance as your Config DB in SSMS, but LogTables shows IsRemoteServer = 1, the Remote Server checkbox was incorrectly ticked when those databases were registered in Nodinite.

Fix this in the Nodinite Web Client:

  1. Navigate to Administration > Settings > Log Databases
  2. Click Edit on each affected Log Database entry
  3. Uncheck the Remote Server checkbox
  4. Click Save
  5. Repeat for all affected entries

Once all entries show IsRemoteServer = 0, re-run the diagnostic query above to confirm, then proceed with the scripts.

Job Step On success On failure What it does
Script A Go to next step Quit — report failure Removes the physical Log Databases from SQL Server
Script B Quit — report success Quit — report failure Removes the database records from the Nodinite Configuration Database

Script A: Remove Databases from SQL Server

Section titled “Script A: Remove Databases from SQL Server”
-- NOTE: @prefix, @configDB, @days, and @managedInstance MUST be identical in Script A and Script B
DECLARE @prefix NVARCHAR(255) = 'Nodinite_Prod_Log_', -- created by v6: 'NodiniteLog_Prod_'
@configDB NVARCHAR(255) = 'Nodinite_Prod_Config', -- created by v6: 'NodiniteConfig_Prod'
@days INT = 180, -- Must be a positive integer
@managedInstance BIT = 0 -- 1 = Azure SQL Managed Instance, 0 = SQL Server
-- @dropSql contains the DROP statements that run in master context
DECLARE @dropSql NVARCHAR(MAX) = 'USE [master]' + CHAR(13) + CHAR(10)
-- @buildSql queries LogTables in the Config DB using a 3-part name so
-- this script can safely run from any database context (e.g. master)
DECLARE @buildSql NVARCHAR(MAX) = N'
SELECT @dropSql +=
''EXEC msdb.dbo.sp_delete_database_backuphistory @database_name = ['' + [Database] + '']'' + CHAR(13) + CHAR(10) +
-- SET SINGLE_USER is not supported on Azure SQL Managed Instance
CASE WHEN @managedInstance = 0
THEN ''ALTER DATABASE ['' + [Database] + ''] SET SINGLE_USER WITH ROLLBACK IMMEDIATE'' + CHAR(13) + CHAR(10)
ELSE '''' END +
''DROP DATABASE ['' + [Database] + '']'' + CHAR(13) + CHAR(10)
FROM [' + @configDB + '].[dbo].[LogTables]
WHERE [Database] LIKE N''' + @prefix + '%''
AND [EndDate] IS NOT NULL
AND [EndDate] < DATEADD(DD, ' + CAST(@days AS VARCHAR(18)) + ' * -1, GetDate())
AND [IsRemoteServer] = @managedInstance -- Managed Instance registers Log Databases with IsRemoteServer = 1
ORDER BY [Database]'
EXEC sp_executesql @buildSql,
N'@dropSql NVARCHAR(MAX) OUTPUT, @managedInstance BIT',
@dropSql OUTPUT, @managedInstance
PRINT @dropSql
-- TODO: Remove the two dashes at the start of the next line when adding to the SQL Agent job
-- EXEC sp_executesql @dropSql
-- NOTE: @prefix, @configDB, @days, and @managedInstance MUST be identical in Script A and Script B
DECLARE @prefix NVARCHAR(255) = 'Nodinite_Prod_Log_', -- created by v6: 'NodiniteLog_Prod_'
@configDB NVARCHAR(255) = 'Nodinite_Prod_Config', -- created by v6: 'NodiniteConfig_Prod'
@days INT = 180, -- Must be a positive integer
@managedInstance BIT = 0 -- 1 = Azure SQL Managed Instance, 0 = SQL Server
DECLARE @sql NVARCHAR(MAX)
SELECT @sql =
'USE [' + @configDB + ']' + CHAR(13)+CHAR(10) +
'DELETE FROM LogTables'
+ ' Where [Database] LIKE (''' + @prefix + '%'')'
+ ' AND [EndDate] IS NOT NULL'
+ ' AND [EndDate] < DATEADD(DD, ' + CAST(@days AS VARCHAR(18)) + ' * -1, GetDate())'
+ ' AND [IsRemoteServer] = ' + CAST(@managedInstance AS CHAR(1)) + CHAR(13)+CHAR(10)
PRINT @sql
-- TODO: Remove the two dashes at the start of the next line when adding to the SQL Agent job
-- EXEC sp_executesql @sql

Before you build the job, verify the scripts return the correct list of databases. Skipping this step risks deleting the wrong databases.

  1. Open SQL Server Management Studio (SSMS)
  2. Connect to the SQL Server where the job will run:
    • If your Log Databases have IsRemoteServer = 0: connect to the Config DB server
    • If your Log Databases have IsRemoteServer = 1: connect to the remote SQL Server that hosts the Log Databases
    • If you run Azure SQL Managed Instance: connect to the Managed Instance and set @managedInstance = 1
  3. Click New Query in the toolbar
  4. Paste in Script A exactly as shown above (with the EXEC line commented out)
  5. Update the four variables at the top (@prefix, @configDB, @days, @managedInstance) to match your environment
  6. Press F5 to run the query
  7. Click the Messages tab at the bottom — you will see a list of database names that would be deleted and the generated DROP DATABASE statements

Once you are satisfied the list is correct, repeat the test for Script B the same way.


Step 2: Open SQL Server Management Studio

Section titled “ Step 2: Open SQL Server Management Studio”
  1. Open SQL Server Management Studio (SSMS). If you do not have it, download it here for free
  2. In the Connect to Server dialog:
    • Server name: the hostname or IP of the SQL Server that hosts your Nodinite Configuration Database
    • Authentication: use Windows Authentication when the server is domain-joined. Use SQL Server Authentication if your DBA gave you a SQL login and password
  3. Click Connect

SQL Server Agent is the built-in job scheduler in SQL Server. Think of it as Windows Task Scheduler, but built directly into SQL Server and far more reliable for database operations. Learn more about SQL Server Agent

In the Object Explorer panel on the left side of SSMS:

  1. Expand the tree node for your SQL Server instance
  2. Scroll down until you see SQL Server Agent
  3. Expand SQL Server Agent
  4. Right-click Jobs
  5. Click New Job...

The New Job dialog opens. On the General tab:

  1. Name: Enter a clear and descriptive name. Good examples:

    • Nodinite - Remove Old Log Databases - Prod
    • Nodinite Log DB Cleanup (Production)
  2. Owner: Set this to a SQL login with sysadmin rights — typically sa or a dedicated DBA service account. Do not use a personal login. If your personal account is ever removed or disabled, the job will stop running silently.

    If your service account is not sysadmin, review SQL Server Sysadmin Requirements and disable DatabaseMaintenance where required.

  3. Category: Click the ... button and select Database Maintenance. This is cosmetic but makes it easy to find the job among dozens of others.

  4. Description: Write a short note about what the job does and why. For example:

    Removes Nodinite Log Databases older than 180 days. Configured per retention policy. Runs every Sunday at 02:00. Created by [your name] on [date].

  5. Make sure the Enabled checkbox is ticked.


Step 5: Add Job Step A — Remove Databases

Section titled “ Step 5: Add Job Step A — Remove Databases”

Click the Steps page on the left side of the New Job dialog, then click New...:

  1. Step name: Step 1 - Remove Old Log Databases from SQL Server

  2. Type: Transact-SQL script (T-SQL)

  3. Run as: Leave as default (SQL Server Agent Service Account)

  4. Database: Select master from the dropdown — the script uses a 3-part database name internally to read from your Config DB, so it can safely run from master context

  5. Command: Paste Script A from above. Before clicking OK, remove the -- comment prefix from the last EXEC line so the script reads:

    EXEC sp_executesql @dropSql

    Also update the four variables at the top to match your environment.

  6. Click the Advanced tab within the step dialog and set:

    • On success action: Go to the next step
    • On failure action: Quit the job reporting failure
  7. Click OK to save the step.


Step 6: Add Job Step B — Clean Up LogTables

Section titled “ Step 6: Add Job Step B — Clean Up LogTables”

Click New... again to add the second step:

  1. Step name: Step 2 - Remove Log Database entries from LogTables

  2. Type: Transact-SQL script (T-SQL)

  3. Run as: Leave as default

  4. Database: Select your Nodinite Configuration Database from the dropdown (e.g., Nodinite_Prod_Config)

  5. Command: Paste Script B from above. Remove the -- comment prefix from the last EXEC line so the script reads:

    EXEC sp_executesql @sql

    Update the four variables to match your environment — they must be identical to the values used in Step 1.

  6. Click the Advanced tab and set:

    • On success action: Quit the job reporting success
    • On failure action: Quit the job reporting failure
  7. Click OK to save the step.


Without a schedule, the job must be started manually every time. A recurring schedule automates the cleanup.

Click the Schedules page, then click New...:

  1. Name: Give the schedule a descriptive name, for example: Weekly Sunday 02:00
  2. Schedule type: Recurring
  3. Frequency section:
    • Occurs: Weekly
    • Recurs every: 1 week(s)
    • Tick the Sunday checkbox (or whichever day your organization uses as a maintenance window)
  4. Daily frequency section:
    • Occurs once at: 02:00:00
  5. Duration section:
    • Start date: today's date
    • End date: No end date
  6. Click OK to save the schedule.

Section titled “ Step 8: Configure Failure Notifications (Recommended)”

Getting an alert when the job fails lets you act before a retention problem grows.

Click the Notifications page:

  1. Tick E-mail
  2. Select an operator (an operator is a named email recipient configured in SQL Server Agent)
  3. Set the condition to When the job fails
  4. Click OK

Click OK at the bottom of the New Job dialog to save.

The job now appears under SQL Server Agent > Jobs in the Object Explorer. Right-click it and select Properties at any time to review or change its configuration.


Before relying on the scheduled run, trigger the job manually once to confirm everything works end-to-end:

  1. In the Object Explorer, expand SQL Server Agent > Jobs
  2. Right-click your job (e.g., Nodinite - Remove Old Log Databases - Prod)
  3. Select Start Job at Step... and choose Step 1 - Remove Old Log Databases from SQL Server
  4. Click Start in the dialog that appears
  5. Wait for the progress indicator to complete — this is usually fast (a few seconds to a minute depending on how many databases are removed)
  6. Click Close

Check that the job completed successfully and that the correct databases were removed:

  1. In the Object Explorer, right-click your job
  2. Select View History
  3. The Job Activity Monitor opens — find your job in the list:
    • ✅ A green checkmark means the job succeeded
    • ❌ A red X means the job failed
  4. Click on any run row to expand it and read the output messages from each step
  5. Refresh the Databases node in the Object Explorer — the removed Log Databases should no longer appear
  6. Open the Nodinite Web Client and navigate to Administration > Settings > Log Databases — deleted entries should be gone from the list

...

Version:
Release date: N/A
Loading...

Explore Nodinite for 30 days – completely free

  • Find out why this is the leading O&M software
  • Prove your business case
  • Test the self-service for the business side

Loading...

See how Nodinite resolves your technical issues, in a LIVE Demo!

Once you submit this request:

  • We will give you a short call to understand the issues you want to tackle.
  • Understand solutions you have tried and why were you not happy with them.
  • Book a convenient slot for a LIVE demo!

Loading...