Hi Brenden,
I agree, better not to move deleted media to recyclebin, but it's up to the customer to decide. I think we'd better add a scheduled task to cleanup the recyclebin, and another big one: the email (attachments).
I don't know what you mean by 'clear out some of the fields'.
The complete stored proc looks like:
CREATE PROCEDURE [dbo].[sp_MediaHistoryCleanup]
@ArchiveDays int = 30
AS
BEGIN
SET NOCOUNT ON;
-- example use:
-- exec sp_MediaHistoryCleanup 30
declare @deleted_rows int
declare @breakbyerror int = 0
declare @ArchiveDate datetime = DATEADD(d,-@ArchiveDays, GetDate())
-- backup log only supported with backupplan full
-- declare @databaseName varchar(1000) = DB_NAME()
print convert(varchar(25), getdate(), 121) + ': Start'
RAISERROR(N'', 0, 1) WITH NOWAIT
set @deleted_rows = 1;
while (@deleted_rows > 0 and @breakbyerror = 0)
begin
begin try
delete top (50) from CMS_ObjectVersionHistory
where VersionObjectType = 'media.file'
and VersionDeletedWhen < @ArchiveDate
set @deleted_rows = @@rowcount;
print convert(varchar(25), getdate(), 121) + ': ' + cast(@deleted_rows as varchar(max)) + ' records deleted'
RAISERROR(N'', 0, 1) WITH NOWAIT
end try
begin catch
select ERROR_NUMBER() AS ErrorNumber
,ERROR_SEVERITY() AS ErrorSeverity
,ERROR_STATE() AS ErrorState
,ERROR_PROCEDURE() AS ErrorProcedure
,ERROR_LINE() AS ErrorLine
,ERROR_MESSAGE() AS ErrorMessage;
print convert(varchar(25), getdate(), 121) + ': Error occurred'
RAISERROR(N'', 0, 1) WITH NOWAIT
set @breakbyerror = 1
end catch
-- backup log @databaseName to disk = N'NUL:'
end
print convert(varchar(25), getdate(), 121) + ': Done'
END
And yes, it takes ages to delete :(