Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 968043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:24:59+00:00 2026-05-16T02:24:59+00:00

I need to delete some text files when a certain database is dropped from

  • 0

I need to delete some text files when a certain database is dropped from SQL server (because they contain associated data that becomes out of date). I had a thought that I could use some sort of trigger to run a query to do that but I can’t figure out where to put that or how to do it.

I’m using SQL server 2005 but likely to upgrade as time goes on.

Any ideas?

thanks,

Jamie

Thanks to Martin Smith, here is the code I used:

CREATE TRIGGER ddl_trig_database  
ON ALL SERVER   
FOR DROP_DATABASE  
AS  
DECLARE @database_count int  
select @database_count=count(*) from sys.databases where name='myDbName'

IF @database_count = 0  
BEGIN  
    -- Our database has been dropped, delete text files  
    DECLARE @Result int  
    DECLARE @FSO_Token int  
    EXEC @Result = sp_OACreate 'Scripting.FileSystemObject', @FSO_Token OUTPUT  
    EXEC @Result = sp_OAMethod @FSO_Token, 'DeleteFile', NULL, 'D:\afile.txt'  
    EXEC @Result = sp_OADestroy @FSO_Token  
END   

I also had to run this to enable the OLE Automation delete:

exec sp_configure
go
exec sp_configure 'Ole Automation Procedures', 1
-- Configuration option 'Ole Automation Procedures' changed from 0 to 1.
go
reconfigure
go
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T02:25:00+00:00Added an answer on May 16, 2026 at 2:25 am

    You need a Server scoped DDL trigger for this I think. I’m not really sure off the top of my head how you would delete text files in TSQL though. Looks like there’s some ideas here. Or maybe you could invoke an SSIS package from the trigger that does the clean up.

    CREATE TRIGGER ddl_trig_database 
    ON ALL SERVER 
    FOR DROP_DATABASE 
    AS 
    
    IF EVENTDATA().value('(/EVENT_INSTANCE/TSQLCommand/CommandText)[1]',
                                                       'nvarchar(max)') = 'myDbName'
    BEGIN
    /*Some code here*/    
    END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to delete two blocks of text from multiple files. The two blocks
I need to delete some data from mysql. I know a bit of php
I need to delete a few fields from the table in the database. I
I need to find a certain chunk from a group of HTML files and
I'm working with richFaces - rich:dataTable. I need to delete some rows and then
I'd like to write some tests for delete() in Grails. These need to be
After eliminating some specific nonsense pattern, I need to identify and possibly delete fields
I need to delete rows in a table, such as DELETE FROM TABLE_X WHERE
I need to delete a picture from a folder where pictures are stored. For
I need to be able to delete a line based on specific text in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.