declare @UsedItems Table
( itemid int primary key, contentID int )
insert into @UsedItems
Select ItemID, ContentID from vefn_GetVersionItemsIncludingDeleted('')

select 'Working Drafts' Query, FolderName,Dv.* from DocVersions DV
JOIN VEFN_GetVersionNames() VN ON DV.VersionID = VN.VersionID

declare @DiscItems Table
( itemid int primary key, ContentID int)
insert into @DiscItems
Select ItemID, II.ContentID from tblItems II
Join tblContents CC ON CC.ContentID = II.ContentID
where ItemID not in(select ItemID from @UsedItems)
And CC.Type is not null

delete from [Revisions] where ItemID in (select itemid from @DiscItems)
delete from [AnnotationAudits] where ItemID in (select itemid from @DiscItems)
delete from [ItemAudits] where ItemID in (select itemid from @DiscItems)
delete from [tblParts] where ItemID in (select itemid from @DiscItems)
delete from [DocVersions] where ItemID in (select itemid from @DiscItems)
delete from [PartAudits] where ItemID in (select itemid from @DiscItems)
delete from [tblAnnotations] where ItemID in (select itemid from @DiscItems)

delete from [Details] where contentID in (select contentid from @DiscItems)
delete from [ZContents] where contentID in (select contentid from @DiscItems)
delete from [tblGrids] where contentID in (select contentid from @DiscItems)
delete from [tblImages] where contentID in (select contentid from @DiscItems)
delete from [ContentAudits] where contentID in (select contentid from @DiscItems)
delete from [tblROUsages] where contentID in (select contentid from @DiscItems)
delete from [EntryAudits] where contentID in (select contentid from @DiscItems)
delete from [tblEntries] where contentID in (select contentid from @DiscItems)
delete from [GridAudits] where contentID in (select contentid from @DiscItems)
delete from [ImageAudits] where contentID in (select contentid from @DiscItems)
delete from [ItemAudits] where contentID in (select contentid from @DiscItems)
delete from [tblParts] where contentID in (select contentid from @DiscItems)
delete from [PartAudits] where contentID in (select contentid from @DiscItems)
delete from [ROUsageAudits] where contentID in (select contentid from @DiscItems)
delete from [ztransitions] where transitionid in(select transitionid from [tblTransitions] where fromid in (select contentid from @DiscItems))
delete from [ztransitions] where transitionid in(select transitionid from [tblTransitions] where toid in (select itemid from @DiscItems))
delete from [ztransitions] where transitionid in(select transitionid from [tblTransitions] where rangeid in (select itemid from @DiscItems))
delete from [tblTransitions] where fromid in (select contentid from @DiscItems)
delete from [tblTransitions] where toid in (select itemid from @DiscItems)
delete from [tblTransitions] where rangeid in (select itemid from @DiscItems)
delete from [tblItems] where PreviousID in (select itemid from @DiscItems)
delete from [tblItems] where ItemID in (select itemid from @DiscItems)
delete from [tblItems] where contentID in (select contentid from @DiscItems)
delete from [tblContents] where contentID in (select contentid from @DiscItems)
