Shrink logfile in sqlserver

Avyayah 1,146 Reputation points

Is it possible to create a job in sqlserver to shrink the datafiles? This is the process I am following:

Step1, Identify the session ID: with this select * from sys.dm_db_task_space_usage
where internal_objects_alloc_page_count <> 0

Kill Session ID, Shrink the logfiles:

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
8,598 questions
No comments
{count} votes

Accepted answer
  1. AmeliaGu-MSFT 13,886 Reputation points

    Hi SahaSaha-5270,
    Agree with Erland. It is better to shrink tempdb after you archiving a lot of data and need to free some disk space. You can shrink the tempdb using dbcc shrinkfile (templog, 'target size in MB') or restart SQL Server. Please refer to this doc which might help.

    Best Regards,

    No comments

2 additional answers

Sort by: Most helpful
  1. Erland Sommarskog 68,621 Reputation points MVP

    Your question is very confused. In the title you talk about log file. But then you go asking about skrinking the data file. And then you list a DMV which relates to tempdb usage?

    I would suggest that whatever you are asking for, the answer is that you should not do it, no matter it is possible or not. Shrinking is an exceptional thing that you do after something exceptional, like moving 90% of the data to an archive database.

    But shrinking something that will start to grow again is perfectly pointless.

  2. Erland Sommarskog 68,621 Reputation points MVP

    Why would that be great? Archive your data. And if the tempdb log is big once you are completed, shrink the log of tempdb (or make it cheap and restart SQL Server).

    But it is completely pointless to shrink the log while you are still working with the archiving. It is only going to delay the process. When SQL Server grows the log, it must zero out the bytes on disk, and that takes time. Why would you want SQL Server to do that again and again and again?

    No comments