- 0 minutes to read

Remove historical Log Databases

Info

On this page, you will learn how to remove historical Nodinite Log Databases using a job in SQL Server.

Nodinite automatically spawns new databases when the conditions from the following System Parameters are met:

Important

Nodinite does NOT automatically remove Log Databases. You must have a routine to remove these according to your policy. There is a very high risk involved when dropping Log Databases, including potential data loss and system instability. Make sure to remove the correct set; Use your own discretion.

The SQL DBA with the proper authority and responsibility may use the sample script on this page to manually or automatically perform the steps required to clean out historical Log Databases as part of planned maintenance. The use of a SQL job in this process ensures reliability and consistency.

Find the oldest number of days to keep messages as defined by the system

It is safe to remove empty old Log Databases. However, even if you have historical Log Databases without any rows in the tables, these still occupy disk space and create overhead, including CPU, disk I/O, pressure on backups and more. Rest assured, the process is designed to minimize any potential risks.

Data retention in the Log Databases is set using some System parameters. Log Events are individually removed based on the Message Type. The following System parameters are in use to specify for how long to keep events and data by default:

Delete Log Databases from SQL Server

Use the following scripts to create or execute the script to remove Log Databases older than the specified time (for example, 180 days) from the Nodinite instance.

SQL Variable Description Example
@prefix The prefix part of Nodinite Log Databases NodiniteLog_Prod_
@configDB The name of the Nodinite Configuration Database NodiniteConfig_Prod
days The number of days to go back in time, a positive integer 180

Follow the sequence of steps as outlined to ensure a successful outcome.

# On success action On failure Action Comment
Step 1 Go to the next step Quit the job reporting failure Remove backup history and delete Old Nodinite Log Databases from SQL Server
Step 2 Quit the job reporting success Quit the job reporting failure Remove Log Database entries from the LogTables table

Step 1: Remove backup history and delete Old Nodinite Log Databases from SQL Server

This step removes the Log Databases from SQL Server.

-- NOTE: You MUST ensure the DECLARE part is the same in Step 1 and Step 2
DECLARE	@prefix NVARCHAR(255) =  'NodiniteLog_Prod_',
	@configDB NVARCHAR(255) = 'NodiniteConfig_Prod',
	@days INT = 180 -- NOTE: Must be a positive integer

DECLARE @sql NVARCHAR(MAX) = 'USE [master]' + CHAR(13)+CHAR(10)  

;WITH nodinitelogdatabases ([Database]) as
(SELECT [Database] From LogTables LT Where LT.[Database] LIKE (@prefix + '%') AND LT.[EndDate] IS NOT NULL AND LT.[EndDate] < DATEADD(DD, @days * -1, GetDate()) AND LT.[IsRemoteServer] = 0 )
	SELECT @sql += 
		'EXEC msdb.dbo.sp_delete_database_backuphistory @database_name = [' + l.[Database] + ']' + CHAR(13)+CHAR(10) +
		'ALTER DATABASE [' + l.[Database] + '] SET SINGLE_USER WITH ROLLBACK IMMEDIATE' + CHAR(13)+CHAR(10) +
		'DROP DATABASE [' + l.[Database] + ']' +  CHAR(13)+CHAR(10)
FROM nodinitelogdatabases l
ORDER BY [Database]
PRINT '-- Number of Log Databases to remove: ' + CAST(@@Rowcount AS VARCHAR(18))

PRINT @sql
-- EXEC sp_executesql @sql

Important

The last line is commented out. Test the output first, then uncomment the last line to perform the operation.

Step 2: Remove Log Database entries from the LogTables table

This step removes the information about Log Databases from Nodinite.

-- NOTE: You MUST ensure the DECLARE part is the same in Step 1 and Step 2
DECLARE	@prefix NVARCHAR(255) =  'NodiniteLog_Prod_',
	@configDB NVARCHAR(255) = 'NodiniteConfig_Prod',
	@days INT = 180 -- NOTE: Must be a positive integer

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] = 0' + CHAR(13)+CHAR(10)  

PRINT @sql
-- EXEC sp_executesql @sql

Important

The last line is commented out. Test the output first, then uncomment the last line to perform the operation.


Next Step

Microsoft SQL Server Options

Log Databases