--First Table--
ALTER TABLE [dbo].[ConnectedPrinter] 
DROP CONSTRAINT [FK_ConnectedPrinter_Printer_AssociatedPrinterId]
GO

ALTER TABLE [dbo].[ConnectedPrinter] 
ADD CONSTRAINT [FK_ConnectedPrinter_Printer_AssociatedPrinterId] FOREIGN KEY ([AssociatedComputerId]) REFERENCES [dbo].[Device] ([DeviceId]) ON DELETE CASCADE;
GO

Update Device
Set Device.LastQueryValuesReceivedDateUtc = device.LastConnectTimeUtc
 where device.DeviceFamilyId = 7;


 Update Device
 Set Device.IsRetired = 1
 where Device.LastQueryValuesReceivedDateUtc < GETDATE() - (select TenantConfig.ReportingDataKeepDays from TenantConfig);

DECLARE @EndDate DateTIme 
--Set to save 1 year of data
SET @EndDate = dateadd(year, -1, getdate())

deleteMore:
DELETE TOP(1000) FROM CompressedDeviceEvents WHERE StartTimeStampUtc < @EndDate;
IF @@ROWCOUNT != 0 goto deleteMore

deleteMore2:
DELETE TOP(1000) FROM DeviceComponentStateDay WHERE StartofDayUTC < @EndDate;
IF @@ROWCOUNT != 0 goto deleteMore2


Truncate table DeviceEventBlob;

DBCC ShrinkDatabase( 'EnterprisePowerManagementDB', 0);

--  Finally, run 'AdminCommand.exe delete_old_data' from Admin command line, while in the Tools folder under Surveyor

